Project

General

Profile

Download (11.3 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

    
53
$ldapcase = $config['system']['webgui']['backend'];
54
switch($ldapcase)
55
{
56
case ldap:
57
     $backing_method="ldap_backed";
58
     break;
59
case ldapother:
60
     $backing_method="ldap_backed";
61
     break;
62
default:
63
    $backing_method="htpasswd_backed"; 
64
}
65

    
66
/* Authenticate user - exit if failed */
67
if (!$auth_method($backing_method)) { exit; }
68

    
69
/* scriptname is set in headjs.php if the user did try to access a page other
70
 * than index.php without beeing logged in.
71
 */
72
if (isset($_POST['scriptname']) && isSystemAdmin($HTTP_SERVER_VARS['AUTH_USER'])) {
73
  pfSenseHeader("{$_POST['scriptname']}");
74
  exit;
75
}
76

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

    
88
  $group = $config['system']['user'][$userindex[$HTTP_SERVER_VARS['AUTH_USER']]]['groupname'];
89
  /* get the group homepage, to be able to forward
90
   * the user to this particular PHP page.
91
   */
92
  getGroupHomePage($group) == "" ? $home = "/index.php" : $home = "/" . getGroupHomePage($group);
93

    
94
  /* okay but if the user realy tries to explicitely access a particular
95
   * page, set $home to that page instead.
96
   */
97
  if (isset($_POST['scriptname']) && $_POST['scriptname'] <> "/" && $_POST['scriptname'] <> "/index.php") {
98
    $home = str_replace('/', '', basename($_POST['scriptname']));
99
    $pagereq = $home;
100
   }
101

    
102
  // If the user is attempting to hit the default page, set it to specifically look for /index.php.
103
  // Without this, any user would have access to the index page.
104
  if ($_SERVER['SCRIPT_NAME'] == '/')
105
  	$_SERVER['SCRIPT_NAME'] = $home;
106
  if ($pagereq == "")
107
	$pagereq = str_replace('/', '', basename($_SERVER['SCRIPT_NAME']));
108

    
109
  // Strip the leading / from the currently requested PHP page
110
  if (!in_array($pagereq,$allowed) && !in_array("ANY", $allowed)) {
111
    // The currently logged in user is not allowed to access the page
112
    // they are attempting to go to.  Redirect them to an allowed page.
113

    
114
    if(stristr($_SERVER['SCRIPT_NAME'],"sajax")) {
115
      echo "||Access to AJAX has been disallowed for this user.";
116
      exit;
117
    }
118
    
119
    if ($pagereq <> "" && (in_array($pagereq, $allowed) || in_array("ANY", $allowed))) {
120
      pfSenseHeader("{$home}");
121
      exit;
122
    } else {
123
      header("HTTP/1.0 401 Unauthorized");
124
      header("Status: 401 Unauthorized");
125

    
126
      echo display_error_form("401", "Unauthorized. You do not have access to the page {$pagereq}");
127
      exit;
128
    }
129
  }
130

    
131
  if (isset($_SESSION['Logged_In'])) {
132
    /*
133
     * only forward if the user has just logged in
134
     * TODO: session auth based - may be an issue.
135
     */
136
    if ($_SERVER['SCRIPT_NAME'] <> $home && empty($_SESSION['First_Visit'])) {
137
      $_SESSION['First_Visit'] = "False";
138
      pfSenseHeader("{$home}");
139
      exit;
140
    }
141
  }
142
}
143

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

    
177
    #login p {
178
      font-size: 1em;
179
      font-weight: bold;
180
      padding: 3px;
181
      margin: 0em;
182
      text-indent: 10px;
183
    }
184

    
185
    #login span {
186
      font-size: 1em;
187
      font-weight: bold;
188
      width: 20%;
189
      padding: 3px;
190
      margin: 0em;
191
      text-indent: 10px;
192
    }
193

    
194
    #login p#text {
195
      font-size: 1em;
196
      font-weight: normal;
197
      padding: 3px;
198
      margin: 0em;
199
      text-indent: 10px;
200
    }
201
    </style>
202

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

    
227
EOD;
228

    
229
  return $htmlstr;
230
}
231

    
232
function display_login_form() {
233
  require_once("globals.inc");
234
  global $config, $g;
235
  $g['theme'] = $config['theme'];
236

    
237
  unset($input_errors);
238

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

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

    
318
?>
(4-4/29)