-
Notifications
You must be signed in to change notification settings - Fork 2
/
login_ldap.php
executable file
·215 lines (181 loc) · 8.32 KB
/
login_ldap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// Set a variable so that misc.inc.php knows not to throw us into an infinite redirect loop
$loginPage = true;
require_once('db.inc.php');
require_once('facilities.inc.php');
// Uncomment these if you need/want to set a title in the header
$header=__("VIO DCIM Login");
$content = "";
$person = new People();
if ( isset($_GET['logout'])) {
// Unfortunately session_destroy() doesn't actually clear out existing variables, so let's nuke from orbit
session_unset();
$_SESSION = array();
unset($_SESSION["userid"]);
session_destroy();
session_commit();
$content = "<h3>Logout successful.</h3>";
}
if ( isset($_POST['username'])) {
$ldapConn = ldap_connect( $config->ParameterArray['LDAPServer'] );
if ( ! $ldapConn ) {
$content = "<h3>Fatal error. The LDAP server is not reachable. Please try again later, or contact your system administrator to check the configuration.</h3>";
error_log( "Unable to connect to LDAP Server: " . $config->ParameterArray['LDAPServer']);
} else {
ldap_set_option( $ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3 );
ldap_set_option( $ldapConn, LDAP_OPT_REFERRALS, 0 );
$ldapUser = htmlspecialchars($_POST['username']);
$ldapDN = str_replace( "%userid%", $ldapUser, $config->ParameterArray['LDAPBindDN']);
$ldapPassword = $_POST['password'];
$ldapBind = ldap_bind( $ldapConn, $ldapDN, $ldapPassword );
if ( ! $ldapBind ) {
$content = "<h3>Login failed. Incorrect username, password, or rights.</h3>";
error_log( __("Unable to bind to specified LDAP server with specified username/password. Username:") . $ldapUser );
} else {
// User was able to authenticate, but might not have authorization to access VIO DCIM. Here we check for those rights.
/* If this install doesn't have the new parameter, use the old default */
if ( !isset($config->ParameterArray['LDAPBaseSearch'])) {
$config->ParameterArray['LDAPBaseSearch'] = "(&(objectClass=posixGroup)(memberUid=%userid%))";
}
$ldapSearchDN = str_replace( "%userid%", $ldapUser, html_entity_decode($config->ParameterArray['LDAPBaseSearch']));
$ldapSearch = ldap_search( $ldapConn, $config->ParameterArray['LDAPBaseDN'], $ldapSearchDN );
$ldapResults = ldap_get_entries( $ldapConn, $ldapSearch );
// Because we have audit logs to maintain, we need to make a local copy of the User's record
// to keep in the VIO DCIM database just in case the user gets removed from LDAP. This also
// makes it easier to check access rights by replicating the user's rights from LDAP into the
// local db for the session. Revoke all rights every login and pull a fresh set from LDAP.
$person->UserID = $ldapUser;
$person->GetPersonByUserID();
$person->revokeAll();
for ( $i = 0; $i < $ldapResults['count']; $i++ ) {
// Originally this was a Switch/Case statement, which would seem to make more sense.
// However, if someone wants to use the same Group identifier for more than one right,
// the switch/case would only allow for that group membership to be used once.
//
// So, here we are with a ton of if/then statements.
if ( $config->ParameterArray['LDAPSiteAccess'] == "" || $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPSiteAccess'] ) {
// No specific group membership required to access VIO DCIM or they have a match to the group required
$_SESSION['userid'] = $ldapUser;
$_SESSION['LoginTime'] = time();
session_commit();
error_log( __("LDAP authentication successful, granted site access based on required group membership. Username:") . $ldapUser);
} else {
error_log( __("LDAP authentication successful, but access denied based on lacking group membership. Username:") . $ldapUser);
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPReadAccess'] ) {
$person->ReadAccess = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPWriteAccess'] ) {
$person->WriteAccess = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPDeleteAccess'] ) {
$person->DeleteAccess = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPAdminOwnDevices'] ) {
$person->AdminOwnDevices = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPRackRequest'] ) {
$person->RackRequest = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPRackAdmin'] ) {
$person->RackAdmin = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPContactAdmin'] ) {
$person->ContactAdmin = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPBulkOperations'] ) {
$person->BulkOperations = true;
}
if ( $ldapResults[$i]['dn'] == $config->ParameterArray['LDAPSiteAdmin'] ) {
$person->SiteAdmin = true;
}
}
// Now get some more info about the user
// Insert the default 4.2 UserSearch string in case this is an upgrade instance
if ( ! isset($config->ParameterArray['LDAPUserSearch'])) {
$config->ParameterArray['LDAPUserSearch'] = "(|(uid=%userid%))";
}
$userSearch = str_replace( "%userid%", $ldapUser, html_entity_decode($config->ParameterArray['LDAPUserSearch']));
$ldapSearch = ldap_search( $ldapConn, $config->ParameterArray['LDAPBaseDN'], $userSearch );
$ldapResults = ldap_get_entries( $ldapConn, $ldapSearch );
// These are standard schema items, so they aren't configurable
// However, suppress any errors that may crop up from not finding them
$person->FirstName = @$ldapResults[0]['givenname'][0];
$person->LastName = @$ldapResults[0]['sn'][0];
$person->Email = @$ldapResults[0]['mail'][0];
if ( isset($_SESSION['userid']) ) {
if ( $person->PersonID > 0 ) {
$person->UpdatePerson();
} else {
$person->CreatePerson();
}
if ( isset($_COOKIE['targeturl'] )) {
header('Location: ' . html_entity_decode($_COOKIE['targeturl']));
} else {
header('Location: ' . redirect('index.php'));
}
exit;
} else {
$content .= "<h3>Login failed. Incorrect username, password, or rights.</h3>";
error_log( __("LDAP Authentication failed for username:") . $ldapUser);
}
}
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>VIO DCIM Data Center Inventory</title>
<link rel="stylesheet" href="css/inventory.php" type="text/css">
<link rel="stylesheet" href="css/jquery-ui.css" type="text/css">
<!--[if lt IE 9]>
<link rel="stylesheet" href="css/ie.css" type="text/css" />
<![endif]-->
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#username").focus();
});
</script>
</head>
<body>
<?php include( 'header.inc.php' ); ?>
<div class="page index">
<?php
include( 'sidebar.inc.php' );
?>
<div class="main" style="box-shadow: 10px 10px #212F39;">
<div class="center"><div>
<?php echo $content; ?>
<form action="login_ldap.php" method="post">
<div class="table">
<div>
<div><label for="username"><?php echo __("Username:"); ?></label></div>
<div><input type="text" id="username" name="username"></div>
</div>
<div>
<div><label for="password"><?php echo __("Password:"); ?></label></div>
<div><input type="password" name="password"></div>
</div>
<div>
<div></div>
<div><input type="submit" name="submit" value="<?php echo __("Submit"); ?>"></div>
</div>
</div>
</form>
<div>
<?php
if ( file_exists("sitecontact.html")) {
include("sitecontact.html");
}
?>
</div>
</div></div>
</div><!-- END div.main -->
</div><!-- END div.page -->
</body>
</html>