Project

General

Profile

Download (11.6 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-2013 BSD Perimeter
10
 * Copyright (c) 2013-2016 Electric Sheep Fencing
11
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
12
 * All rights reserved.
13
 *
14
 * 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
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * 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
 */
26

    
27
include_once("auth.inc");
28
include_once("priv.inc");
29
if (!function_exists('platform_booting')) {
30
	require_once('globals.inc');
31
}
32
require_once('pfsense-utils.inc');
33

    
34
/* Authenticate user - exit if failed */
35
if (!session_auth()) {
36
	display_login_form();
37
	exit;
38
}
39

    
40
phpsession_begin();
41

    
42
/*
43
 * Once here, the user has authenticated with the web server.
44
 * We give them access only to the appropriate pages based on
45
 * the user or group privileges.
46
 */
47
$allowedpages = getAllowedPages($_SESSION['Username'], $_SESSION['user_radius_attributes']);
48

    
49
/*
50
 * Get user-based preference settings so they can be easily referenced.
51
 */
52
$user_settings = get_user_settings($_SESSION['Username']);
53

    
54
/*
55
 * redirect to first allowed page if requesting a wrong url
56
 */
57

    
58
/* Fix this up otherwise the privilege check will fail. See Redmine #5909. */
59
if ($_SERVER['REQUEST_URI'] == "/") {
60
	$_SERVER['REQUEST_URI'] = "/index.php";
61
}
62

    
63
if (!isAllowedPage($_SERVER['REQUEST_URI'])) {
64
	if (count($allowedpages) > 0) {
65
		$page = str_replace('*', '', $allowedpages[0]);
66
		$_SESSION['Post_Login'] = true;
67
		require_once("functions.inc");
68
		pfSenseHeader("/{$page}");
69

    
70
		$username = get_config_user();
71
		log_error("{$username} attempted to access {$_SERVER['SCRIPT_NAME']} but does not have access to that page. Redirecting to {$page}.");
72

    
73
		exit;
74
	} else {
75
		// add this so they don't get stuck on the logout page when they have no permissions.
76
		$_SESSION["Logged_In"] = false;
77
		display_error_form("201", gettext("No page assigned to this user! Click here to logout."));
78

    
79
		exit;
80
	}
81
} else {
82
	$_SESSION['Post_Login'] = true;
83
}
84

    
85
/*
86
 * redirect browsers post-login to avoid pages
87
 * taking action in response to a POST request
88
 */
89
if (!$_SESSION['Post_Login']) {
90
	$_SESSION['Post_Login'] = true;
91
	require_once("functions.inc");
92
	pfSenseHeader($_SERVER['REQUEST_URI']);
93
	exit;
94
}
95

    
96
/*
97
 * Close session data to allow other scripts from same host to come in.
98
 * A session can be reactivated from calling phpsession_begin again
99
 */
100
phpsession_end(true);
101

    
102
/*
103
 * determine if the user is allowed access to the requested page
104
 */
105
function display_error_form($http_code, $desc) {
106
	global $config, $user_settings, $g;
107

    
108
	if (isAjax()) {
109
		printf(gettext('Error: %1$s Description: %2$s'), $http_code, $desc);
110
		return;
111
	}
112

    
113
	$logincssfile = "#770101";
114
?>
115

    
116
<!DOCTYPE html>
117
<html lang="en">
118
	<head>
119
		<meta name="viewport" content="width=device-width, initial-scale=1">
120
	    <link rel="stylesheet" href="/vendor/bootstrap/css/bootstrap.min.css" type="text/css">
121
	    <link rel="stylesheet" href="/css/login.css?v=<?=filemtime('/usr/local/www/css/login.css')?>" type="text/css">
122
		<title><?=gettext("Error"); ?></title>
123
	</head>
124

    
125
	<body id="error" >
126
		<div id="total">
127
			<header>
128
				<div id="headerrow">
129
					<div class="row">
130
						<div class="col-sm-4">
131
							<div id="logodiv" style="text-align:center" class="nowarning">
132
								<?php include("/usr/local/www/logo.svg"); ?>
133
							</div>
134
						</div>
135
						<div class="col-sm-8 nowarning msgbox text-center">
136
							<span id="hostspan">
137
							</span>
138
						</div>
139
					</div>
140
				</div>
141
			</header>
142

    
143
			<div style="background: <?=$logincssfile?>;" class="pagebody">
144
				<div class="col-sm-2"></div>
145

    
146
				<div class="col-sm-8 offset-md-4 logoCol">
147
					<div class="loginCont center-block error-panel">
148
						<a href="index.php?logout"><?=$desc;?></a>
149
					</div>
150
				</div>
151

    
152
			<div class="col-sm-2"></div>
153
			</div>
154

    
155
			<footer id="3">
156
			<div id="footertext">
157
					<p class="text-muted">
158
						<?=print_credit()?>
159
					</p>
160
				</div>
161
			</footer>
162
		</div>
163
	</body>
164
</html>
165

    
166
<?php
167

    
168
} // end function
169

    
170

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

    
175
	unset($input_errors);
176

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

    
194
	/* Check against locally configured IP addresses, which will catch when someone
195
	   port forwards WebGUI access from WAN to an internal IP on the router. */
196
	global $FilterIflist, $nifty_background;
197

    
198
	$local_ip = false;
199

    
200
	if (strpos($_SERVER['HTTP_HOST'], ":") === FALSE) {
201
		$http_host_port = explode(":", $_SERVER['HTTP_HOST']);
202
		$http_host = $http_host_port[0];
203
	} else {
204
		$http_host = $_SERVER['HTTP_HOST'];
205
	}
206

    
207
	if (empty($FilterIflist)) {
208
		require_once('filter.inc');
209
		require_once('shaper.inc');
210
		filter_generate_optcfg_array();
211
	}
212

    
213
	foreach ($FilterIflist as $iflist) {
214
		if ($iflist['ip'] == $http_host) {
215
			$local_ip = true;
216
		} else if ($iflist['ipv6'] == $http_host) {
217
			$local_ip = true;
218
		} else if (is_array($iflist['vips'])) {
219
			foreach ($iflist['vips'] as $vip) {
220
				if ($vip['ip'] == $http_host) {
221
					$local_ip = true;
222
					break;
223
				}
224
			}
225

    
226
			unset($vip);
227
		}
228

    
229
		if ($local_ip == true) {
230
			break;
231
		}
232
	}
233

    
234
	unset($FilterIflist);
235
	unset($iflist);
236

    
237
	if ($local_ip == false) {
238
		if (is_array($config['openvpn']['openvpn-server'])) {
239
			foreach ($config['openvpn']['openvpn-server'] as $ovpns) {
240
				if (is_ipaddrv4($http_host) && !empty($ovpns['tunnel_network']) && ip_in_subnet($http_host, $ovpns['tunnel_network'])) {
241
					$local_ip = true;
242
				} else if (is_ipaddrv6($http_host) && !empty($ovpns['tunnel_networkv6']) && ip_in_subnet($http_host, $ovpns['tunnel_networkv6'])) {
243
					$local_ip = true;
244
				}
245

    
246
				if ($local_ip == true) {
247
					break;
248
				}
249
			}
250
		}
251
	}
252

    
253
	// For the login form, get the settings of no particular user.
254
	// That ensures we will use the system default theme for the login form.
255
	$user_settings = get_user_settings("");
256
	$cssfile = "/css/pfSense.css";
257

    
258
	if (isset($user_settings['webgui']['webguicss'])) {
259
		if (file_exists("/usr/local/www/css/" . $user_settings['webgui']['webguicss'])) {
260
			$cssfile = "/css/" . $user_settings['webgui']['webguicss'];
261
		}
262
	}
263

    
264
	$logincssfile = "#1e3f75";
265

    
266
	if (isset($user_settings['webgui']['logincss']) && strlen($user_settings['webgui']['logincss']) == 6) {
267
		$logincssfile = "#" . $user_settings['webgui']['logincss'];
268
	}
269

    
270
	if (isset($config['system']['webgui']['loginshowhost'])) {
271
		$loginbannerstr = sprintf(gettext('%1$s.%2$s'), htmlspecialchars($config['system']['hostname']), htmlspecialchars($config['system']['domain']));
272
		$login_title = gettext(htmlspecialchars($config['system']['hostname'] . " - Login"));
273
	} else {
274
		$loginbannerstr = sprintf(gettext('Login to %1$s'), $g['product_name']);
275
		$login_title = gettext("pfSense - Login");
276
	}
277

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

    
280
	if (is_ipaddr($http_host) && !$local_ip && !isset($config['system']['webgui']['nohttpreferercheck'])) {
281
		$warnclass = "pagebodywarn";	// Make room for a warning display row
282
	} else {
283
		$warnclass = "pagebody";
284
	}
285
?>
286
<!DOCTYPE html>
287
<html lang="en">
288
	<head>
289
		<meta name="viewport" content="width=device-width, initial-scale=1">
290
	    <link rel="stylesheet" href="/vendor/bootstrap/css/bootstrap.min.css" type="text/css">
291
	    <link rel="stylesheet" href="/css/login.css?v=<?=filemtime('/usr/local/www/css/login.css')?>" type="text/css">
292
		<title><?=$login_title; ?></title>
293
		<script type="text/javascript">
294
			//<![CDATA{
295
			var events = events || [];
296
			//]]>
297
		</script>
298
	</head>
299

    
300
	<body id="login" >
301
		<div id="total">
302
			<header>
303
				<div id="headerrow">
304
					<div class="row">
305
						<!-- Header left logo box -->
306
						<div class="col-sm-4">
307
							<div id="logodiv" style="text-align:center" class="nowarning">
308
								<?php include("/usr/local/www/logo.svg"); ?>
309
							</div>
310
						</div>
311

    
312
						<!-- Header center message box -->
313
						<div class="col-sm-4 nowarning msgbox text-center text-danger">
314
<?php
315
						if (!empty($_POST['usernamefld'])) {
316
							print("<h4>" . $_SESSION['Login_Error'] . "</h4>");
317
						}
318
?>
319
						</div>
320

    
321
						<!-- Header right message box (hostname or msg)-->
322
						<div class="col-sm-4 nowarning msgbox text-center">
323
							<span id="hostspan">
324
								<a><h4><?=$loginbannerstr?></h4></a>
325
							</span>
326
						</div>
327
					</div>
328
<?php
329
	if ($warnclass == "pagebodywarn") {
330
?>
331
					<div class="row">
332
						<div class="col-sm-12">
333
							<div class="alert alert-warning <?=$warnclass?>">
334
								<?=gettext("The IP address being used to access this router is not configured locally, which may be forwarded by NAT or other means.
335
								If this forwarding is unexpected, it should be verified that a man-in-the-middle attack is not taking place.")?>
336
							</div>
337
						</div>
338
					</div>
339
<?php
340
	}
341
?>
342
	            </div>
343
	        </header>
344

    
345
	        <div style="background: <?=$logincssfile?>;" class="<?=$warnclass?>">
346
	        	<div class="col-sm-4"></div>
347

    
348
	        	<div class="col-sm-4 offset-md-4 logoCol">
349
					<div class="loginCont center-block">
350
		                <form method="post" <?=$loginautocomplete?> class="login">
351
			                <p class="form-title">Sign In</p>
352
			                <input name="usernamefld" id="usernamefld" type="text" placeholder="Username" autocorrect="off" autocapitalize="none"/>
353
			                <input name="passwordfld" id="passwordfld" type="password" placeholder="Password" />
354
			                <input type="submit" name="login" value="Sign In" class="btn btn-success btn-sm" />
355
		                </form>
356
					</div>
357
	            </div>
358

    
359
	        	<div class="col-sm-4"></div>
360
	        </div>
361

    
362
	        <footer id="3">
363
	            <div id="footertext">
364
					<p class="text-muted">
365
						<?=print_credit()?>
366
					</p>
367
	            </div>
368
	        </footer>
369
	    </div>
370

    
371
	    <script src="/vendor/jquery/jquery-3.4.1.min.js?v=<?=filemtime('/usr/local/www/vendor/jquery/jquery-3.4.1.min.js')?>"></script>
372
		<script src="/vendor/bootstrap/js/bootstrap.min.js?v=<?=filemtime('/usr/local/www/vendor/bootstrap/js/bootstrap.min.js')?>"></script>
373
		<script src="/js/pfSense.js?v=<?=filemtime('/usr/local/www/js/pfSense.js')?>"></script>
374

    
375
		<script type="text/javascript">
376
		//!<[CDATA[
377
		events.push(function() {
378
			document.cookie=
379
				"cookie_test=1" +
380
				"<?php echo $config['system']['webgui']['protocol'] == 'https' ? '; secure' : '';?>";
381

    
382
			if (document.cookie.indexOf("cookie_test") == -1) {
383
				alert("<?=gettext('The browser must support cookies to login.')?>");
384
			}
385

    
386
			// Delete it
387
			document.cookie = "cookie_test=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
388
		});
389
		//]]>
390
		</script>
391

    
392
	</body>
393
</html>
394

    
395
<?php
396
} // end function
(5-5/60)