Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * authgui.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>
7
 * Copyright (c) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
8
 * Copyright (c) 2006 Paul Taylor <paultaylor@winn-dixie.com>
9
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
10
 * All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions are met:
14
 *
15
 * 1. Redistributions of source code must retain the above copyright notice,
16
 *    this list of conditions and the following disclaimer.
17
 *
18
 * 2. Redistributions in binary form must reproduce the above copyright
19
 *    notice, this list of conditions and the following disclaimer in
20
 *    the documentation and/or other materials provided with the
21
 *    distribution.
22
 *
23
 * 3. All advertising materials mentioning features or use of this software
24
 *    must display the following acknowledgment:
25
 *    "This product includes software developed by the pfSense Project
26
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
27
 *
28
 * 4. The names "pfSense" and "pfSense Project" must not be used to
29
 *    endorse or promote products derived from this software without
30
 *    prior written permission. For written permission, please contact
31
 *    coreteam@pfsense.org.
32
 *
33
 * 5. Products derived from this software may not be called "pfSense"
34
 *    nor may "pfSense" appear in their names without prior written
35
 *    permission of the Electric Sheep Fencing, LLC.
36
 *
37
 * 6. Redistributions of any form whatsoever must retain the following
38
 *    acknowledgment:
39
 *
40
 * "This product includes software developed by the pfSense Project
41
 * for use in the pfSense software distribution (http://www.pfsense.org/).
42
 *
43
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55
 */
56

    
57
include_once("auth.inc");
58
include_once("priv.inc");
59
if (!function_exists('platform_booting')) {
60
	require_once('globals.inc');
61
}
62

    
63
/* Authenticate user - exit if failed */
64
if (!session_auth()) {
65
	display_login_form();
66
	exit;
67
}
68

    
69
/*
70
 * Once here, the user has authenticated with the web server.
71
 * We give them access only to the appropriate pages based on
72
 * the user or group privileges.
73
 */
74
$allowedpages = getAllowedPages($_SESSION['Username'], $_SESSION['user_radius_attributes']);
75

    
76
/*
77
 * Get user-based preference settings so they can be easily referenced.
78
 */
79
$user_settings = get_user_settings($_SESSION['Username']);
80

    
81
/*
82
 * redirect to first allowed page if requesting a wrong url
83
 */
84

    
85
/* Fix this up otherwise the privilege check will fail. See Redmine #5909. */
86
if ($_SERVER['REQUEST_URI'] == "/") {
87
	$_SERVER['REQUEST_URI'] = "/index.php";
88
}
89

    
90
if (!isAllowedPage($_SERVER['REQUEST_URI'])) {
91
	if (count($allowedpages) > 0) {
92
		$page = str_replace('*', '', $allowedpages[0]);
93
		$_SESSION['Post_Login'] = true;
94
		require_once("functions.inc");
95
		pfSenseHeader("/{$page}");
96

    
97
		$username = empty($_SESSION["Username"]) ? "(system)" : $_SESSION['Username'];
98
		if (!empty($_SERVER['REMOTE_ADDR'])) {
99
			$username .= '@' . $_SERVER['REMOTE_ADDR'];
100
		}
101
		log_error("{$username} attempted to access {$_SERVER['SCRIPT_NAME']} but does not have access to that page. Redirecting to {$page}.");
102

    
103
		exit;
104
	} else {
105
		display_error_form("201", gettext("No page assigned to this user! Click here to logout."));
106
		exit;
107
	}
108
} else {
109
	$_SESSION['Post_Login'] = true;
110
}
111

    
112
/*
113
 * redirect browsers post-login to avoid pages
114
 * taking action in response to a POST request
115
 */
116
if (!$_SESSION['Post_Login']) {
117
	$_SESSION['Post_Login'] = true;
118
	require_once("functions.inc");
119
	pfSenseHeader($_SERVER['REQUEST_URI']);
120
	exit;
121
}
122

    
123
/*
124
 * Close session data to allow other scripts from same host to come in.
125
 * A session can be reactivated from calling session_start again
126
 */
127
session_commit();
128

    
129
/*
130
 * determine if the user is allowed access to the requested page
131
 */
132
function display_error_form($http_code, $desc) {
133
	global $config, $user_settings, $g;
134

    
135
	if (isAjax()) {
136
		printf(gettext('Error: %1$s Description: %2$s'), $http_code, $desc);
137
		return;
138
	}
139

    
140
	$cssfile = "/css/pfSense.css";
141

    
142
	if (isset($user_settings['webgui']['webguicss'])) {
143
		if (file_exists("/usr/local/www/css/" . $user_settings['webgui']['webguicss'])) {
144
			$cssfile = "/css/" . $user_settings['webgui']['webguicss'];
145
		}
146
	}
147

    
148
?>
149
<!DOCTYPE html>
150
<html lang="en">
151
<head>
152
	<meta name="viewport" content="width=device-width, initial-scale=1">
153
	<link rel="stylesheet" href="<?=$cssfile?>" />
154
	<title><?=gettext("Error: not allowed"); ?></title>
155
</head>
156
<body id="error" class="no-menu">
157
	<div id="jumbotron">
158
		<div class="container">
159
			<div class="col-sm-offset-3 col-sm-6 col-xs-12">
160
				<!-- FIXME: We really need to POST the logout action -->
161
				<div class="alert alert-danger" role="alert"><a href="index.php?logout"><?=$desc;?></a></div>
162
			</div>
163
		</div>
164
	</div>
165
</body>
166
</html>
167
<?php
168

    
169
} // end function
170

    
171

    
172
function display_login_form() {
173
	require_once("globals.inc");
174
	global $config, $g;
175

    
176
	unset($input_errors);
177

    
178
	if (isAjax()) {
179
		if (isset($_POST['login'])) {
180
			if ($_SESSION['Logged_In'] <> "True") {
181
				isset($_SESSION['Login_Error']) ? $login_error = $_SESSION['Login_Error'] : $login_error = gettext("unknown reason");
182
				printf("showajaxmessage('" . gettext("Invalid login (%s).") . "')", $login_error);
183
			}
184
			if (file_exists("{$g['tmp_path']}/webconfigurator.lock")) {
185
				// TODO: add the IP from the user who did lock the device
186
				$whom = file_get_contents("{$g['tmp_path']}/webconfigurator.lock");
187
				printf("showajaxmessage('" . gettext("This device is currently being maintained by: %s.") . "');", $whom);
188
			}
189
		}
190
		exit;
191
	}
192

    
193
/* Check against locally configured IP addresses, which will catch when someone
194
   port forwards WebGUI access from WAN to an internal IP on the router. */
195
global $FilterIflist, $nifty_background;
196
$local_ip = false;
197
if (strpos($_SERVER['HTTP_HOST'], ":") === FALSE) {
198
	$http_host_port = explode(":", $_SERVER['HTTP_HOST']);
199
	$http_host = $http_host_port[0];
200
} else {
201
	$http_host = $_SERVER['HTTP_HOST'];
202
}
203
if (empty($FilterIflist)) {
204
	require_once('filter.inc');
205
	require_once('shaper.inc');
206
	filter_generate_optcfg_array();
207
}
208
foreach ($FilterIflist as $iflist) {
209
	if ($iflist['ip'] == $http_host) {
210
		$local_ip = true;
211
	} else if ($iflist['ipv6'] == $http_host) {
212
		$local_ip = true;
213
	} else if (is_array($iflist['vips'])) {
214
		foreach ($iflist['vips'] as $vip) {
215
			if ($vip['ip'] == $http_host) {
216
				$local_ip = true;
217
				break;
218
			}
219
		}
220
		unset($vip);
221
	}
222
	if ($local_ip == true) {
223
		break;
224
	}
225
}
226
unset($FilterIflist);
227
unset($iflist);
228

    
229
if ($local_ip == false) {
230
	if (is_array($config['openvpn']['openvpn-server'])) {
231
		foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
232
			if (is_ipaddrv4($http_host) && !empty($ovpns['tunnel_network']) && ip_in_subnet($http_host, $ovpns['tunnel_network'])) {
233
				$local_ip = true;
234
			} else if (is_ipaddrv6($http_host) && !empty($ovpns['tunnel_networkv6']) && ip_in_subnet($http_host, $ovpns['tunnel_networkv6'])) {
235
				$local_ip = true;
236
			}
237
			if ($local_ip == true) {
238
				break;
239
			}
240
		}
241
	}
242
}
243

    
244
// For the login form, get the settings of no particular user.
245
// That ensures we will use the system default theme for the login form.
246
$user_settings = get_user_settings("");
247
$cssfile = "/css/pfSense.css";
248

    
249
if (isset($user_settings['webgui']['webguicss'])) {
250
	if (file_exists("/usr/local/www/css/" . $user_settings['webgui']['webguicss'])) {
251
		$cssfile = "/css/" . $user_settings['webgui']['webguicss'];
252
	}
253
}
254

    
255
?>
256
<!DOCTYPE html>
257
<html lang="en">
258
<head>
259
	<meta name="viewport" content="width=device-width, initial-scale=1">
260
	<link rel="stylesheet" href="<?=$cssfile?>" />
261
	<title><?=gettext("Login"); ?></title>
262
	<script type="text/javascript">
263
	//<![CDATA{
264
	var events = events || [];
265
	//]]>
266
	</script>
267
</head>
268
<body id="login" class="no-menu">
269
	<div id="jumbotron">
270
		<div class="container">
271
			<div class="col-sm-offset-3 col-sm-6 col-xs-12">
272
<?php
273
	if (is_ipaddr($http_host) && !$local_ip && !isset($config['system']['webgui']['nohttpreferercheck'])) {
274
		$nifty_background = "#999";
275
		print_info_box(gettext("The IP address being used to access this router is not configured locally, which may be forwarded by NAT or other means. <br /><br />If this forwarding is unexpected, it should be verified that a man-in-the-middle attack is not taking place."));
276
	}
277

    
278
	$loginautocomplete = isset($config['system']['webgui']['loginautocomplete']) ? '' : 'autocomplete="off"';
279
?>
280

    
281
				<div class="panel panel-default">
282
					<div class="panel-heading">
283
						<h2 class="panel-title"><?=sprintf(gettext("Login to %s"), $g['product_name'])?></h2>
284
					</div>
285

    
286
					<div class="panel-body">
287
<?php if (!empty($_SESSION['Login_Error'])): ?>
288
						<div class="alert alert-danger" role="alert"><?=$_SESSION['Login_Error'];?></div>
289
<?php endif ?>
290
						<div class="alert alert-warning hidden" id="no_cookies"><?= gettext("The browser must support cookies to login."); ?></div>
291

    
292
						<form method="post" <?= $loginautocomplete ?> action="<?=$_SERVER['SCRIPT_NAME'];?>" class="form-horizontal">
293
							<div class="form-group">
294
								<label for="usernamefld" class="col-sm-3 control-label"><?=gettext("Username")?></label>
295
								<div class="col-sm-9 col-md-7">
296
									<input type="text" class="form-control" name="usernamefld" id="usernamefld" placeholder="<?=gettext("Enter your username")?>" autocorrect="off" autocapitalize="none" spellcheck="false">
297
								</div>
298
							</div>
299

    
300
							<div class="form-group">
301
								<label for="passwordfld" class="col-sm-3 control-label"><?=gettext("Password")?></label>
302
								<div class="col-sm-9 col-md-7">
303
									<input type="password" class="form-control" name="passwordfld" id="passwordfld" placeholder="<?=gettext("Enter your password")?>">
304
								</div>
305
							</div>
306

    
307
							<div class="form-group">
308
								<div class="col-sm-offset-3 col-sm-9 col-md-7">
309
									<button type="submit" class="btn btn-primary" name="login"><?=gettext("Login")?></button>
310
								</div>
311
							</div>
312
						</form>
313
					</div>
314
				</div>
315
			</div>
316
		</div>
317

    
318
		<script type="text/javascript">
319
		//!<[CDATA[
320
		events.push(function() {
321
			document.cookie=
322
				"cookie_test=1" +
323
				"<?php echo $config['system']['webgui']['protocol'] == 'https' ? '; secure' : '';?>";
324

    
325
			if (document.cookie.indexOf("cookie_test") == -1)
326
				document.getElementById("no_cookies").style.display="";
327
			else
328
				document.getElementById("no_cookies").style.display="none";
329

    
330
			// Delete it
331
			document.cookie = "cookie_test=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
332
		});
333
		//]]>
334
		</script>
335
<?php
336
require_once('foot.inc');
337

    
338
} // end function
(5-5/65)