r1624 + stamp RackTables release 0.14.9
[racktables] / inc / auth.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3
4Authentication library for RackTables.
5
6*/
7
8// This function ensures that we don't continue without a legitimate
9// username and password.
10function authenticate ()
11{
e673ee24
DO
12 if
13 (
14 !isset ($_SERVER['PHP_AUTH_USER']) or
15 !isset ($_SERVER['PHP_AUTH_PW']) or
4eb5efb7
DO
16 !authenticated ($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) or
17 isset ($_REQUEST['logout'])
e673ee24
DO
18 )
19 {
9c0b0016 20 header ('WWW-Authenticate: Basic realm="' . getConfigVar ('enterprise') . ' RackTables access"');
e673ee24
DO
21 header ('HTTP/1.0 401 Unauthorized');
22 showError ('This system requires authentication. You should use a username and a password.');
23 die();
24 }
25}
26
27// Show error unless the user is allowed access here.
28function authorize ()
29{
30 global $remote_username, $pageno, $tabno;
31 if (!authorized ($remote_username, $pageno, $tabno))
32 {
33 showError ("User '${remote_username}' is not allowed to access here.");
34 die();
35 }
36}
37
38// This function returns TRUE, if username and password are valid.
39function authenticated ($username, $password)
40{
41 global $accounts;
e673ee24
DO
42 if ($accounts[$username]['user_enabled'] != 'yes')
43 return FALSE;
7dfd5e44
DO
44 // Always authenticate the administrator locally, thus giving him a chance
45 // to fix broken installation.
46 if ($accounts[$username]['user_id'] == 1)
47 return authenticated_via_database ($username, $password);
48 switch (getConfigVar ('USER_AUTH_SRC'))
49 {
50 case 'database':
51 return authenticated_via_database ($username, $password);
52 break;
53 case 'ldap':
54 return authenticated_via_ldap ($username, $password);
55 break;
56 default:
57 showError ("Unknown user authentication source configured.", __FUNCTION__);
58 return FALSE;
59 break;
60 }
61 // and just to be sure...
62 return FALSE;
63}
64
65function authenticated_via_ldap ($username, $password)
66{
67 return FALSE;
68}
69
70function authenticated_via_database ($username, $password)
71{
72 global $accounts;
73 if (!defined ('HASH_HMAC'))
74 {
75 showError ('Fatal error: PHP hash extension is missing', __FUNCTION__);
76 die();
77 }
78 if (array_search (PASSWORD_HASH, hash_algos()) === FALSE)
79 {
80 showError ('Password hash not supported, authentication impossible.', __FUNCTION__);
81 die();
82 }
83 if (!isset ($accounts[$username]['user_password_hash']))
84 return FALSE;
e673ee24
DO
85 if ($accounts[$username]['user_password_hash'] == hash (PASSWORD_HASH, $password))
86 return TRUE;
87 return FALSE;
88}
89
90// This function returns TRUE, if specified user has access to the
91// page and tab.
92function authorized ($username, $pageno, $tabno)
93{
94 global $perms;
95 // Deny access by default, then accumulate all corrections from database.
96 // Order of nested cycles is important here!
97 // '%' as page or tab name has a special value and means "any".
98 // 0 as user_id means "any user".
99 $answer = 'no';
100 foreach (array ('%', $username) as $u)
101 foreach (array ('%', $tabno) as $t)
102 foreach (array ('%', $pageno) as $p)
103 if (isset ($perms[$u][$p][$t]))
104 $answer = $perms[$u][$p][$t];
105 if ($answer == 'yes')
106 return TRUE;
107 return FALSE;
108}
109
110// This function returns password hash for given user ID.
111function getHashByID ($user_id = 0)
112{
113 if ($user_id <= 0)
114 {
b09549b3 115 showError ('Invalid user_id', __FUNCTION__);
e673ee24
DO
116 return NULL;
117 }
118 global $accounts;
119 foreach ($accounts as $account)
120 if ($account['user_id'] == $user_id)
121 return $account['user_password_hash'];
122 return NULL;
123}
124
125?>