summaryrefslogtreecommitdiffstats
path: root/etc/inc/captiveportal.inc
blob: 0f91be97fd67189d2af8da6c2988afabf9893f90 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<?php
/* $Id$ */
/*
	captiveportal.inc
	part of pfSense (http://www.pfSense.com)

	Copyright (C) 2005 Scott Ullrich <sullrich@gmail.com>
	All rights reserved.
        
        originally part of m0n0wall (http://m0n0.ch/wall)
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
	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.

	This version of captiveportal.inc has been modified by Rob Parker
	<rob.parker@keycom.co.uk> to include changes for per-user bandwidth management
	via returned RADIUS attributes. This page has been modified to delete any
	added rules which may have been created by other per-user code (index.php, etc).
	These changes are (c) 2004 Keycom PLC.
*/

/* include all configuration functions */
require_once("functions.inc");
require_once("radius_accounting.inc") ;

function captiveportal_configure() {
	global $config, $g;

	if (isset($config['captiveportal']['enable']) &&
		(($config['captiveportal']['interface'] == "lan") ||
			isset($config['interfaces'][$config['captiveportal']['interface']]['enable']))) {

		if($g['booting']) echo "Starting captive portal... ";

		/* kill any running mini_httpd */
		killbypid("{$g['varrun_path']}/mini_httpd.cp.pid");
		killbypid("{$g['varrun_path']}/mini_httpd.cps.pid");

		/* kill any running minicron */
		killbypid("{$g['varrun_path']}/minicron.pid");

		/* stop accounting on all clients */
		captiveportal_radius_stop_all() ;

		/* remove old information */
		unlink_if_exists("{$g['vardb_path']}/captiveportal.nextrule");
		unlink_if_exists("{$g['vardb_path']}/captiveportal.db");
		unlink_if_exists("{$g['vardb_path']}/captiveportal_mac.db");
		unlink_if_exists("{$g['vardb_path']}/captiveportal_ip.db");
		unlink_if_exists("{$g['vardb_path']}/captiveportal_radius.db");

		/* write portal page */
		if ($config['captiveportal']['page']['htmltext'])
			$htmltext = base64_decode($config['captiveportal']['page']['htmltext']);
		else {
			/* example/template page */
			$htmltext = <<<EOD
<html>
<title>pfSense's captive portal</title>
<head>
 <STYLE type="text/css">
.listhdrr {
	background-color: #BBBBBB;
	padding-right: 16px;
	padding-left: 6px;
	font-weight: bold;
	border-right: 1px solid #999999;
	border-bottom: 1px solid #999999;
	font-size: 11px;
	padding-top: 5px;
	padding-bottom: 5px;
}

 </STYLE>
</head>
<body bgcolor="#990000">
<center>
<font color="white" face="arial" size="+1">Welcome to pfSense's captive portal!</font>
<p>
<form method="post" action="\$PORTAL_ACTION\$">
<table border="0" cellpadding="6" cellspacing="0">
<tr><td align="right" class="listhdrr"><font color="white">Username:</td><td class="listhdrr"><input name="auth_user" type="text"></td></tr>
<tr><td align="right" class="listhdrr"><font color="white">Password:</td><td class="listhdrr"><input name="auth_pass" type="password"></td></tr>
<input name="redirurl" type="hidden" value="\$PORTAL_REDIRURL\$">
</table>
<p>
<center><input name="accept" type="submit" value="Continue">
</form>
</center>
</body>
</html>

EOD;
		}

		$fd = @fopen("{$g['varetc_path']}/captiveportal.html", "w");
		if ($fd) {
			fwrite($fd, $htmltext);
			fclose($fd);
		}

		/* write error page */
		if ($config['captiveportal']['page']['errtext'])
			$errtext = base64_decode($config['captiveportal']['page']['errtext']);
		else {
			/* example page */
			$errtext = <<<EOD
<html>
<head>
<title>Authentication error</title>
</head>
<body>
<font color="#cc0000"><h2>Authentication error</h2></font>
<b>
Username and/or password invalid.
<br><br>
<a href="javascript:history.back()">Go back</a>
</b>
</body>
</html>

EOD;
		}

		$fd = @fopen("{$g['varetc_path']}/captiveportal-error.html", "w");
		if ($fd) {
			fwrite($fd, $errtext);
			fclose($fd);
		}

		chdir($g['captiveportal_path']);

		/* start web server */
		mwexec("/usr/local/sbin/mini_httpd -a -M 0 -u root -maxproc 16" .
			" -p 8000 -i {$g['varrun_path']}/mini_httpd.cp.pid");

		/* fire up another one for HTTPS if requested */
		if (isset($config['captiveportal']['httpslogin']) &&
			$config['captiveportal']['certificate'] && $config['captiveportal']['private-key']) {

			$cert = base64_decode($config['captiveportal']['certificate']);
			$key = base64_decode($config['captiveportal']['private-key']);

			$fd = fopen("{$g['varetc_path']}/cert-portal.pem", "w");
			if (!$fd) {
				printf("Error: cannot open cert-portal.pem in system_webgui_start().\n");
				return 1;
			}
			chmod("{$g['varetc_path']}/cert-portal.pem", 0600);
			fwrite($fd, $cert);
			fwrite($fd, "\n");
			fwrite($fd, $key);
			fclose($fd);

			mwexec("/usr/local/sbin/mini_httpd -S -a -M 0 -E {$g['varetc_path']}/cert-portal.pem" .
				" -u root -maxproc 16 -p 8001" .
				" -i {$g['varrun_path']}/mini_httpd.cps.pid");
		}

		/* start pruning process (interval = 60 seconds) */
		mwexec("/usr/local/bin/minicron 60 {$g['varrun_path']}/minicron.pid " .
			"/etc/rc.prunecaptiveportal");

		/* generate passthru mac database */
		captiveportal_passthrumac_configure() ;
		/* create allowed ip database and insert pf tables to make it so */
		captiveportal_allowedip_configure() ;

		/* generate radius server database */
		if($config['captiveportal']['radiusip'] && $config['captiveportal']['auth_method']=="radius") {
			$radiusip = $config['captiveportal']['radiusip'] ;

			if($config['captiveportal']['radiusport'])
				$radiusport = $config['captiveportal']['radiusport'] ;
			else
				$radiusport = 1812;

			if($config['captiveportal']['radiusacctport'])
				$radiusacctport = $config['captiveportal']['radiusacctport'] ;
			else
				$radiusacctport = 1813;

			$radiuskey = $config['captiveportal']['radiuskey'];

			$fd = @fopen("{$g['vardb_path']}/captiveportal_radius.db", "w");
			if (!$fd) {
				printf("Error: cannot open radius DB file in captiveportal_configure().\n");
				return 1;
			} else {
				fwrite($fd,$radiusip . "," . $radiusport . "," . $radiusacctport . "," . $radiuskey) ;
			}
			fclose($fd) ;
		}

		if($g['booting']) print "done.\n";

	} else {
		killbypid("{$g['varrun_path']}/mini_httpd.cp.pid");
		killbypid("{$g['varrun_path']}/minicron.pid");
		captiveportal_radius_stop_all() ;
	}

	return 0;
}

/* remove clients that have been around for longer than the specified amount of time */
/* db file structure: timestamp,clientip,clientmac,username,sessionid */
function captiveportal_prune_old() {

	global $g, $config;

	/* check for expired entries */
	if ($config['captiveportal']['timeout'])
		$timeout = $config['captiveportal']['timeout'] * 60;
	else
		$timeout = 0;

	if ($config['captiveportal']['idletimeout'])
		$idletimeout = $config['captiveportal']['idletimeout'] * 60;
	else
		$idletimeout = 0;

	if (!$timeout && !$idletimeout)
		return;

	captiveportal_lock();

	/* read database */
	$cpdb = captiveportal_read_db();

	$radiusservers = captiveportal_get_radius_servers();

	for ($i = 0; $i < count($cpdb); $i++) {

		$timedout = false;

		/* hard timeout? */
		if ($timeout) {
			if ((time() - $cpdb[$i][0]) >= $timeout)
				$timedout = true;
		}

		/* if an idle timeout is specified, get last activity timestamp from pf */
		// XXX: we need a solution for this.
		if (!$timedout && $idletimeout) {
			$isactive = captiveportal_get_last_activity($cpdb[$i][2]);
			if($isactive == 0)
				$timedout = true;
		}

		if ($timedout) {
			/* this client needs to be deleted - remove pf table item */
			if (isset($config['captiveportal']['radacct_enable']) && isset($radiusservers[0])) {
				RADIUS_ACCOUNTING_STOP($cpdb[$i][1], // ruleno
									   $cpdb[$i][4], // username
									   $cpdb[$i][5], // sessionid
									   $cpdb[$i][0], // start time
									   $radiusservers[0]['ipaddr'],
									   $radiusservers[0]['acctport'],
									   $radiusservers[0]['key'],
									   $cpdb[$i][2]); //clientip
				syslog(LOG_INFO,"Authenticated user $cpdb[$i][4] timed out");
			}
						
			mwexec("/sbin/pfctl -t captiveportal -T delete {$cpdb[$i][2]}");

			unset($cpdb[$i]);
		}
	}

	/* write database */
	captiveportal_write_db($cpdb);

	captiveportal_unlock();
}

/* remove a single client */
function captiveportal_disconnect_client($id) {

	global $g, $config;

	captiveportal_lock();

	/* read database */
	$cpdb = captiveportal_read_db();
	$radiusservers = captiveportal_get_radius_servers();

	/* find entry */
	for ($i = 0; $i < count($cpdb); $i++) {
		if ($cpdb[$i][1] == $id) {
			/* this client needs to be deleted - remove pf table item */
			if (isset($config['captiveportal']['radacct_enable']) && isset($radiusservers[0])) {
				RADIUS_ACCOUNTING_STOP($cpdb[$i][1], // ruleno
									   $cpdb[$i][4], // username
									   $cpdb[$i][5], // sessionid
									   $cpdb[$i][0], // start time
									   $radiusservers[0]['ipaddr'],
									   $radiusservers[0]['acctport'],
									   $radiusservers[0]['key'],
									   $cpdb[$i][2]); //clientip
				syslog(LOG_INFO,"Authenticated user $cpdb[$i][4] disconnected");
			}

			unset($cpdb[$i]);

			break;
		}
	}

	/* write database */
	captiveportal_write_db($cpdb);

	captiveportal_unlock();
}

/* send RADIUS acct stop for all current clients */
function captiveportal_radius_stop_all() {
	global $g, $config;

	captiveportal_lock() ;
	$cpdb = captiveportal_read_db() ;

	$radiusservers = captiveportal_get_radius_servers();

	if (isset($radiusservers[0])) {
		for ($i = 0; $i < count($cpdb); $i++) {
			RADIUS_ACCOUNTING_STOP($cpdb[$i][1], // ruleno
								   $cpdb[$i][4], // username
								   $cpdb[$i][5], // sessionid
								   $cpdb[$i][0], // start time
								   $radiusservers[0]['ipaddr'],
								   $radiusservers[0]['acctport'],
								   $radiusservers[0]['key'],
								   $cpdb[$i][2]); //clientip
		}
	}
	captiveportal_unlock() ;
}

function captiveportal_passthrumac_configure() {
	global $config, $g;

	/* clear out passthru macs, if necessary */
	if (file_exists("{$g['vardb_path']}/captiveportal_mac.db")) {
		unlink("{$g['vardb_path']}/captiveportal_mac.db");
	}

	if (is_array($config['captiveportal']['passthrumac'])) {

		$fd = @fopen("{$g['vardb_path']}/captiveportal_mac.db", "w");
		if (!$fd) {
			printf("Error: cannot open passthru mac DB file in captiveportal_passthrumac_configure().\n");
			return 1;
		}

		foreach ($config['captiveportal']['passthrumac'] as $macent) {
			/* record passthru mac so it can be recognized and let thru */
			fwrite($fd, $macent['mac'] . "\n");
		}

		fclose($fd);
	}

	return 0;
}

function captiveportal_allowedip_configure() {
	global $config, $g;

	captiveportal_lock() ;

	/* clear out existing allowed ips, if necessary */
	if (file_exists("{$g['vardb_path']}/captiveportal_ip.db")) {
		$fd = @fopen("{$g['vardb_path']}/captiveportal_ip.db", "r");
		if ($fd) {
			while (!feof($fd)) {
				$line = trim(fgets($fd));
				if($line) {
					list($ip,$rule) = explode(",",$line);
					mwexec("/sbin/pfctl -t captiveportal -T delete {$ip}");
				}
			}
		}
		fclose($fd) ;
		unlink("{$g['vardb_path']}/captiveportal_ip.db");
	}

	if (is_array($config['captiveportal']['allowedip'])) {

		$fd = @fopen("{$g['vardb_path']}/captiveportal_ip.db", "w");
		if (!$fd) {
			printf("Error: cannot open allowed ip DB file in captiveportal_allowedip_configure().\n");
			captiveportal_unlock() ;
			return 1;
		}

		foreach ($config['captiveportal']['allowedip'] as $ipent) {
			/* record allowed ip so it can be recognized and removed later */
			fwrite($fd, $ipent['ip'] . "," . $ruleno ."\n");

			/* insert pf table item to allow traffic */
			mwexec("echo \"pfctl -t captiveportal -T add {$ipent['ip']} \"> /tmp/tmp");
			echo "Adding {$ipent['ip']}";
			mwexec("/sbin/pfctl -t captiveportal -T add {$ipent['ip']}");

			$ruleno = $ip;
		}

		fclose($fd);

		/* write next rule number */
		$fd = @fopen("{$g['vardb_path']}/captiveportal.nextrule", "w");
		if ($fd) {
			fwrite($fd, $ruleno);
			fclose($fd);
		}
	}

	captiveportal_unlock() ;
	return 0;
}

/* get last activity timestamp given pf table item */
function captiveportal_get_last_activity($ip) {

	$info = `/usr/sbin/arp | /usr/bin/grep $ip`;

	if($info <> "") return 1;

	return 0;
}

/* read captive portal DB into array */
function captiveportal_read_db() {

	global $g;

	$cpdb = array();
	$fd = @fopen("{$g['vardb_path']}/captiveportal.db", "r");
	if ($fd) {
		while (!feof($fd)) {
			$line = trim(fgets($fd));
			if ($line) {
				$cpdb[] = explode(",", $line);
			}
		}
		fclose($fd);
	}
	return $cpdb;
}

/* write captive portal DB */
function captiveportal_write_db($cpdb) {

	global $g;

	$fd = @fopen("{$g['vardb_path']}/captiveportal.db", "w");
	if ($fd) {
		foreach ($cpdb as $cpent) {
			fwrite($fd, join(",", $cpent) . "\n");
		}
		fclose($fd);
	}
}

/* read RADIUS servers into array */
function captiveportal_get_radius_servers() {

	global $g;

	if (file_exists("{$g['vardb_path']}/captiveportal_radius.db")) {
	   	$fd = @fopen("{$g['vardb_path']}/captiveportal_radius.db","r");
		if ($fd) {
			$radiusservers = array();
			while (!feof($fd)) {
				$line = trim(fgets($fd));
				if ($line) {
					$radsrv = array();
					list($radsrv['ipaddr'],$radsrv['port'],$radsrv['acctport'],$radsrv['key']) = explode(",",$line);
					$radiusservers[] = $radsrv;
				}
			}
			fclose($fd);

			return $radiusservers;
		}
	}

	return false;
}

/* lock captive portal information, decide that the lock file is stale after
   10 seconds */
function captiveportal_lock() {

	global $g;

	$lockfile = "{$g['varrun_path']}/captiveportal.lock";

	$n = 0;
	while ($n < 10) {
		/* open the lock file in append mode to avoid race condition */
		if ($fd = @fopen($lockfile, "x")) {
			/* succeeded */
			fclose($fd);
			return;
		} else {
			/* file locked, wait and try again */
			sleep(1);
			$n++;
		}
	}
}

/* unlock configuration file */
function captiveportal_unlock() {

	global $g;

	$lockfile = "{$g['varrun_path']}/captiveportal.lock";

	if (file_exists($lockfile))
		unlink($lockfile);
}

?>
OpenPOWER on IntegriCloud