-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnityLDAP.php
320 lines (269 loc) · 8.7 KB
/
UnityLDAP.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
namespace UnityWebPortal\lib;
use PHPOpenLDAPer\LDAPConn;
use PHPOpenLDAPer\LDAPEntry;
/**
* An LDAP connection class which extends ldapConn tailored for the Unity Cluster
*/
class UnityLDAP extends ldapConn
{
// User Specific Constants
private const ID_MAP = array(1000, 9999);
private const PI_ID_MAP = array(10000, 19999);
private const ORG_ID_MAP = array(20000, 29999);
private const RDN = "cn"; // The defauls RDN for LDAP entries is set to "common name"
public const POSIX_ACCOUNT_CLASS = array(
"inetorgperson",
"posixAccount",
"top",
"ldapPublicKey"
);
public const POSIX_GROUP_CLASS = array(
"posixGroup",
"top"
);
// string vars for OUs
private $STR_USEROU;
private $STR_GROUPOU;
private $STR_PIGROUPOU;
private $STR_ORGGROUPOU;
private $STR_ADMINGROUP;
// Instance vars for various ldapEntry objects
private $userOU;
private $groupOU;
private $pi_groupOU;
private $org_groupOU;
private $adminGroup;
private $custom_mappings_path;
private $def_user_shell;
public function __construct(
$host,
$dn,
$pass,
$custom_user_mappings,
$user_ou,
$group_ou,
$pigroup_ou,
$orggroup_ou,
$admin_group,
$def_user_shell
) {
parent::__construct($host, $dn, $pass);
$this->STR_USEROU = $user_ou;
$this->STR_GROUPOU = $group_ou;
$this->STR_PIGROUPOU = $pigroup_ou;
$this->STR_ORGGROUPOU = $orggroup_ou;
$this->STR_ADMINGROUP = $admin_group;
// Get Global Entries
$this->userOU = $this->getEntry($user_ou);
$this->groupOU = $this->getEntry($group_ou);
$this->pi_groupOU = $this->getEntry($pigroup_ou);
$this->org_groupOU = $this->getEntry($orggroup_ou);
$this->adminGroup = $this->getEntry($admin_group);
$this->custom_mappings_path = $custom_user_mappings;
$this->def_user_shell = $def_user_shell;
}
//
// Get methods for OU
//
public function getUserOU()
{
return $this->userOU;
}
public function getGroupOU()
{
return $this->groupOU;
}
public function getPIGroupOU()
{
return $this->pi_groupOU;
}
public function getOrgGroupOU()
{
return $this->org_groupOU;
}
public function getAdminGroup()
{
return $this->adminGroup;
}
public function getDefUserShell()
{
return $this->def_user_shell;
}
//
// ID Number selection functions
//
public function getNextUIDNumber(UnitySQL $UnitySQL): int
{
$new_id = $UnitySQL->getSiteVar('MAX_UID') + 1;
while ($this->IDNumInUse($new_id)) {
$new_id++;
}
$UnitySQL->updateSiteVar('MAX_UID', $new_id);
return $new_id;
}
public function getNextPiGIDNumber(UnitySQL $UnitySQL): int
{
$max_pigid = $UnitySQL->getSiteVar('MAX_PIGID');
$new_pigid = $max_pigid + 1;
while ($this->IDNumInUse($new_pigid)) {
$new_pigid++;
}
$UnitySQL->updateSiteVar('MAX_PIGID', $new_pigid);
return $new_pigid;
}
public function getNextOrgGIDNumber(UnitySQL $UnitySQL): int
{
$max_gid = $UnitySQL->getSiteVar('MAX_GID');
$new_gid = $max_gid + 1;
while ($this->IDNumInUse($new_gid)) {
$new_gid++;
}
$UnitySQL->updateSiteVar('MAX_GID', $new_gid);
return $new_gid;
}
private function IDNumInUse(int $id): bool
{
// id reserved for debian packages
if (($id >= 100 && $id <= 999) || ($id >= 60000 && $id <= 64999)) {
return true;
}
$users = $this->userOU->getChildrenArray(true);
foreach ($users as $user) {
if ($user["uidnumber"][0] == $id) {
return true;
}
}
$pi_groups = $this->pi_groupOU->getChildrenArray(true);
foreach ($pi_groups as $pi_group) {
if ($pi_group["gidnumber"][0] == $id) {
return true;
}
}
$groups = $this->groupOU->getChildrenArray(true);
foreach ($groups as $group) {
if ($group["gidnumber"][0] == $id) {
return true;
}
}
return false;
}
public function getUnassignedID($uid, $UnitySQL)
{
$netid = strtok($uid, "_"); // extract netid
// scrape all files in custom folder
$dir = new \DirectoryIterator($this->custom_mappings_path);
foreach ($dir as $fileinfo) {
if ($fileinfo->getExtension() == "csv") {
// found csv file
$handle = fopen($fileinfo->getPathname(), "r");
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$netid_match = $data[0];
$uid_match = $data[1];
if ($uid == $netid_match || $netid == $netid_match) {
// found a match
if (!$this->IDNumInUse($uid_match)) {
return $uid_match;
}
}
}
}
}
// didn't find anything from existing mappings, use next available
$next_uid = $this->getNextUIDNumber($UnitySQL);
return $next_uid;
}
//
// Functions that return user/group objects
//
public function getAllUsers($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();
if (!$ignorecache) {
$users = $UnityRedis->getCache("sorted_users", "");
if (!is_null($users)) {
foreach ($users as $user) {
array_push($out, new UnityUser($user, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook));
}
return $out;
}
}
$users = $this->userOU->getChildren(true);
foreach ($users as $user) {
$params = array($user->getAttribute("cn")[0], $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook);
array_push($out, new UnityUser(...$params));
}
return $out;
}
public function getAllPIGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();
if (!$ignorecache) {
$groups = $UnityRedis->getCache("sorted_groups", "");
if (!is_null($groups)) {
foreach ($groups as $group) {
$params = array($group, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook);
array_push($out, new UnityGroup(...$params));
}
return $out;
}
}
$pi_groups = $this->pi_groupOU->getChildren(true);
foreach ($pi_groups as $pi_group) {
array_push($out, new UnityGroup(
$pi_group->getAttribute("cn")[0],
$this,
$UnitySQL,
$UnityMailer,
$UnityRedis,
$UnityWebhook
));
}
return $out;
}
public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook, $ignorecache = false)
{
$out = array();
if (!$ignorecache) {
$orgs = $UnityRedis->getCache("sorted_orgs", "");
if (!is_null($orgs)) {
foreach ($orgs as $org) {
array_push($out, new UnityOrg($org, $this, $UnitySQL, $UnityMailer, $UnityRedis, $UnityWebhook));
}
return $out;
}
}
$org_groups = $this->org_groupOU->getChildren(true);
foreach ($org_groups as $org_group) {
array_push($out, new UnityOrg(
$org_group->getAttribute("cn")[0],
$this,
$UnitySQL,
$UnityMailer,
$UnityRedis,
$UnityWebhook
));
}
return $out;
}
public function getUserEntry($uid)
{
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$uid," . $this->STR_USEROU);
return $ldap_entry;
}
public function getGroupEntry($gid)
{
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_GROUPOU);
return $ldap_entry;
}
public function getPIGroupEntry($gid)
{
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_PIGROUPOU);
return $ldap_entry;
}
public function getOrgGroupEntry($gid)
{
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
return $ldap_entry;
}
}