Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
		Copyright (C) 2007, 2008 Scott Ullrich <sullrich@gmail.com>
5
		All rights reserved.
6

    
7
        Copyright (C) 2005-2006 Bill Marquette <bill.marquette@gmail.com>
8
        All rights reserved.
9

    
10
        Copyright (C) 2006 Paul Taylor <paultaylor@winn-dixie.com>.
11
        All rights reserved.
12

    
13
        Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
14
        All rights reserved.
15

    
16
        Redistribution and use in source and binary forms, with or without
17
        modification, are permitted provided that the following conditions are met:
18

    
19
        1. Redistributions of source code must retain the above copyright notice,
20
           this list of conditions and the following disclaimer.
21

    
22
        2. Redistributions in binary form must reproduce the above copyright
23
           notice, this list of conditions and the following disclaimer in the
24
           documentation and/or other materials provided with the distribution.
25

    
26
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
        POSSIBILITY OF SUCH DAMAGE.
36
*/
37

    
38
include_once("auth.inc");
39
require_once("functions.inc");
40

    
41
/* We only support htpasswd backed HTTP Basic auth and session
42
 * based backing methods at the moment.
43
 * 		session_auth - this will use session based authentication and timeout
44
 *      htpasswd_backed - this uses the "standard" .htpasswd file
45
 *      passwd_backed - this will use the system passwd file in /etc
46
 *      radius_backed - this will allow you to use a radius server
47
 *      pam_backed - this uses the system's PAM facility .htpasswd file
48
 */
49
$auth_method="session_auth";
50

    
51
/* enable correct auth backend, default to htpasswd_backed */
52
$ldapcase = $config['system']['webgui']['backend'];
53
switch($ldapcase)
54
{
55
case ldap:
56
     $backing_method="ldap_backed";
57
     break;
58
case ldapother:
59
     $backing_method="ldap_backed";
60
     break;
61
default:
62
    $backing_method="htpasswd_backed"; 
63
}
64

    
65

    
66
//if($config['system']['webgui']['backend'] == "ldap"){
67
//	$backing_method="ldap_backed";
68
//} elseif($config['system']['webgui']['backend'] == "ldapother")
69
//        $backing_method="ldap_backed";
70
//else
71
//	$backing_method="htpasswd_backed";
72

    
73
/* Authenticate user - exit if failed */
74
if (!$auth_method($backing_method)) { exit; }
75

    
76
/* scriptname is set in headjs.php if the user did try to access a page other
77
 * than index.php without beeing logged in.
78
 */
79
if (isset($_POST['scriptname']) && isSystemAdmin($HTTP_SERVER_VARS['AUTH_USER'])) {
80
  pfSenseHeader("{$_POST['scriptname']}");
81
  exit;
82
}
83

    
84
// Once here, the user has authenticated with the web server.
85
// Now, we give them access only to the appropriate pages for their group.
86
if (!(isSystemAdmin($HTTP_SERVER_VARS['AUTH_USER']))) {
87
	$_SESSION['privs'] = getAllowedGroups($HTTP_SERVER_VARS['AUTH_USER']);
88
	$allowed = $_SESSION['privs'];
89
	
90
	$allowed_groups = print_r($_SESSION['privs'],true);
91
	$fdny = fopen("/tmp/groups", "w");
92
	fwrite($fdny, $allowed_groups);
93
	fclose($fdny);
94

    
95
  $group = $config['system']['user'][$userindex[$HTTP_SERVER_VARS['AUTH_USER']]]['groupname'];
96
  /* get the group homepage, to be able to forward
97
   * the user to this particular PHP page.
98
   */
99
  getGroupHomePage($group) == "" ? $home = "/index.php" : $home = "/" . getGroupHomePage($group);
100

    
101
  /* okay but if the user realy tries to explicitely access a particular
102
   * page, set $home to that page instead.
103
   */
104
  if (isset($_POST['scriptname']) && $_POST['scriptname'] <> "/" && $_POST['scriptname'] <> "/index.php") {
105
    $home = str_replace('/', '', basename($_POST['scriptname']));
106
    $pagereq = $home;
107
   }
108

    
109
  // If the user is attempting to hit the default page, set it to specifically look for /index.php.
110
  // Without this, any user would have access to the index page.
111
  if ($_SERVER['SCRIPT_NAME'] == '/')
112
  	$_SERVER['SCRIPT_NAME'] = $home;
113
  if ($pagereq == "")
114
	$pagereq = str_replace('/', '', basename($_SERVER['SCRIPT_NAME']));
115

    
116
  // Strip the leading / from the currently requested PHP page
117
  if (!in_array($pagereq,$allowed) && !in_array("ANY", $allowed)) {
118
    // The currently logged in user is not allowed to access the page
119
    // they are attempting to go to.  Redirect them to an allowed page.
120

    
121
    if(stristr($_SERVER['SCRIPT_NAME'],"sajax")) {
122
      echo "||Access to AJAX has been disallowed for this user.";
123
      exit;
124
    }
125
    
126
    if ($pagereq <> "" && (in_array($pagereq, $allowed) || in_array("ANY", $allowed))) {
127
      pfSenseHeader("{$home}");
128
      exit;
129
    } else {
130
      header("HTTP/1.0 401 Unauthorized");
131
      header("Status: 401 Unauthorized");
132

    
133
      echo display_error_form("401", "Unauthorized. You do not have access to the page {$pagereq}");
134
      exit;
135
    }
136
  }
137

    
138
  if (isset($_SESSION['Logged_In'])) {
139
    /*
140
     * only forward if the user has just logged in
141
     * TODO: session auth based - may be an issue.
142
     */
143
    if ($_SERVER['SCRIPT_NAME'] <> $home && empty($_SESSION['First_Visit'])) {
144
      $_SESSION['First_Visit'] = "False";
145
      pfSenseHeader("{$home}");
146
      exit;
147
    }
148
  }
149
}
150

    
151
function display_error_form($http_code, $desc) {
152
  global $config, $g;
153
  $g['theme'] = $config['theme'];
154
	if(isAjax()) {
155
		echo "Error: {$http_code} Description: {$desc}";
156
		return;
157
	}
158
  $htmlstr = <<<EOD
159
<html>
160
  <head>
161
    <script type="text/javascript" src="/javascript/scriptaculous/prototype.js"></script>
162
    <script type="text/javascript" src="/javascript/scriptaculous/scriptaculous.js"></script>
163
    <title>An error occurred: {$http_code}</title>
164
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
165
    <link rel="shortcut icon" href="/themes/{$g['theme']}/images/icons/favicon.ico" />
166
    <link rel="stylesheet" type="text/css" href="/themes/{$g['theme']}/all.css" media="all" />
167
    <style type="text/css">
168
    #errordesc {
169
      background: #cccccc;
170
      border: 0px solid #666666;
171
      margin: 5em auto;
172
      padding: 0em;
173
      width: 340px;
174
    }
175
    #errordesc h1 {
176
      background: url(/themes/{$g['theme']}/images/misc/logon.png) no-repeat top left;
177
      margin-top: 0;
178
      display: block;
179
      text-indent: -1000px;
180
      height: 50px;
181
      border-bottom: none;
182
    }
183

    
184
    #login p {
185
      font-size: 1em;
186
      font-weight: bold;
187
      padding: 3px;
188
      margin: 0em;
189
      text-indent: 10px;
190
    }
191

    
192
    #login span {
193
      font-size: 1em;
194
      font-weight: bold;
195
      width: 20%;
196
      padding: 3px;
197
      margin: 0em;
198
      text-indent: 10px;
199
    }
200

    
201
    #login p#text {
202
      font-size: 1em;
203
      font-weight: normal;
204
      padding: 3px;
205
      margin: 0em;
206
      text-indent: 10px;
207
    }
208
    </style>
209

    
210
    <script type="text/javascript">
211
    <!--
212
      function page_load() {
213
        NiftyCheck();
214
        Rounded("div#errordesc","bl br","#333","#cccccc","smooth");
215
        Effect.Pulsate('errortext', { duration: 10 });
216
      }
217
      <?php
218
        require("headjs.php");
219
        echo getHeadJS();
220
      ?>
221
    //-->
222
    </script>
223
    <script type="text/javascript" src="/themes/{$g['theme']}/javascript/niftyjsCode.js"></script>
224
  </head>
225
  <body onload="page_load();">
226
    <div id="errordesc">
227
      <h1>&nbsp</h1>
228
      <a href="/">
229
      <p id="errortext" style="vertical-align: middle; text-align: center;"><span style="color: #000000; font-weight: bold;">{$desc}</span></p>
230
    </div>
231
  </body>
232
</html>
233

    
234
EOD;
235

    
236
  return $htmlstr;
237
}
238

    
239
function display_login_form() {
240
  require_once("globals.inc");
241
  global $config, $g;
242
  $g['theme'] = $config['theme'];
243

    
244
  unset($input_errors);
245

    
246
  if(isAjax()) {
247
    if (isset($_POST['login'])) {
248
      if($_SESSION['Logged_In'] <> "True") {
249
        isset($_SESSION['Login_Error']) ? $login_error = $_SESSION['Login_Error'] : $login_error = "unknown reason";
250
        echo "showajaxmessage('Invalid login ({$login_error}).');";
251
      }
252
      if (file_exists("{$g['tmp_path']}/webconfigurator.lock")) {
253
        // TODO: add the IP from the user who did lock the device
254
        $whom = file_get_contents("{$g['tmp_path']}/webconfigurator.lock");
255
        echo "showajaxmessage('This device is currently beeing maintained by: {$whom}.');";
256
      }
257
    }
258
    exit;
259
  }
260

    
261
?>
262
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
263
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
264
<html>
265
  <head>
266
    <script type="text/javascript" src="/javascript/scriptaculous/prototype.js"></script>
267
    <script type="text/javascript" src="/javascript/scriptaculous/scriptaculous.js"></script>
268
    <title><?=gettext("Login"); ?></title>
269
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
270
    <link rel="shortcut icon" href="/themes/<?= $g['theme'] ?>/images/icons/favicon.ico" />
271
    <?php if (file_exists("{$g['www_path']}/themes/{$g['theme']}/login.css")): ?>
272
    <link rel="stylesheet" type="text/css" href="/themes/<?= $g['theme'] ?>/login.css" media="all" />
273
    <?php else: ?>
274
    <link rel="stylesheet" type="text/css" href="/themes/<?= $g['theme'] ?>/all.css" media="all" />
275
    <?php endif; ?>
276
    <script type="text/javascript">
277
    <!--
278
      function page_load() {
279
        NiftyCheck();
280
        Rounded("div#login","bl br","#333","#cccccc","smooth");
281
        document.login_iform.usernamefld.focus();
282
      }
283
      function clearError() {
284
        if($('inputerrors'))
285
          $('inputerrors').innerHTML='';
286
      }
287
      <?php
288
        require("headjs.php");
289
        echo getHeadJS();
290
      ?>
291
    //-->
292
    </script>
293
    <script type="text/javascript" src="/themes/<?= $g['theme'] ?>/javascript/niftyjsCode.js"></script>
294
  </head>
295
  <body onload="page_load()">
296
    <div id="login">
297
      <h1>&nbsp;</h1>
298
      <form id="iform" name="login_iform" method="post" autocomplete="off" action="<?= $_SERVER['SCRIPT_NAME'] ?>">
299
        <div id="inputerrors"></div>
300
        <p>
301
          <span style="text-align: left;width=40%">
302
            <?=gettext("Username"); ?>:&nbsp;&nbsp;
303
          <input onclick="clearError();" onchange="clearError();" id="usernamefld" type="text" name="usernamefld" class="formfld user" tabindex="1" />
304
          </span>
305
        </p>
306
        <p>
307
          <span style="text-align: left;width=40%;">
308
            <?=gettext("Password"); ?>:&nbsp;&nbsp;
309
          </span>
310
          <input onclick="clearError();" onchange="clearError();" id="passwordfld" type="password" name="passwordfld" class="formfld pwd" tabindex="2" />
311
        </p>
312
        <table width="90%" style="margin-right: auto; margin-left: auto;">
313
          <tr>
314
            <td valign="middle" align="right" style="font-style: italic;"><br /><?=gettext("Enter username and password to login."); ?></td>
315
            <td valign="middle" align="left"><input type="submit" id="submit" name="login" class="formbtn" value="<?=gettext("Login"); ?>" tabindex="3" /></td>
316
          </tr>
317
        </table>
318
      </form>
319
    </div>
320
  </body>
321
</html>
322
<?php
323
} // end function
324

    
325
?>
(4-4/29)