@@ -38,1331 +38,1331 @@ |
||
| 38 | 38 | use OC\ServerNotAvailableException; |
| 39 | 39 | |
| 40 | 40 | class Wizard extends LDAPUtility { |
| 41 | - /** @var \OCP\IL10N */ |
|
| 42 | - static protected $l; |
|
| 43 | - protected $access; |
|
| 44 | - protected $cr; |
|
| 45 | - protected $configuration; |
|
| 46 | - protected $result; |
|
| 47 | - protected $resultCache = array(); |
|
| 48 | - |
|
| 49 | - const LRESULT_PROCESSED_OK = 2; |
|
| 50 | - const LRESULT_PROCESSED_INVALID = 3; |
|
| 51 | - const LRESULT_PROCESSED_SKIP = 4; |
|
| 52 | - |
|
| 53 | - const LFILTER_LOGIN = 2; |
|
| 54 | - const LFILTER_USER_LIST = 3; |
|
| 55 | - const LFILTER_GROUP_LIST = 4; |
|
| 56 | - |
|
| 57 | - const LFILTER_MODE_ASSISTED = 2; |
|
| 58 | - const LFILTER_MODE_RAW = 1; |
|
| 59 | - |
|
| 60 | - const LDAP_NW_TIMEOUT = 4; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Constructor |
|
| 64 | - * @param Configuration $configuration an instance of Configuration |
|
| 65 | - * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
| 66 | - * @param Access $access |
|
| 67 | - */ |
|
| 68 | - public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
| 69 | - parent::__construct($ldap); |
|
| 70 | - $this->configuration = $configuration; |
|
| 71 | - if(is_null(Wizard::$l)) { |
|
| 72 | - Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
| 73 | - } |
|
| 74 | - $this->access = $access; |
|
| 75 | - $this->result = new WizardResult(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function __destruct() { |
|
| 79 | - if($this->result->hasChanges()) { |
|
| 80 | - $this->configuration->saveConfiguration(); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * counts entries in the LDAP directory |
|
| 86 | - * |
|
| 87 | - * @param string $filter the LDAP search filter |
|
| 88 | - * @param string $type a string being either 'users' or 'groups'; |
|
| 89 | - * @return bool|int |
|
| 90 | - * @throws \Exception |
|
| 91 | - */ |
|
| 92 | - public function countEntries($filter, $type) { |
|
| 93 | - $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
| 94 | - if($type === 'users') { |
|
| 95 | - $reqs[] = 'ldapUserFilter'; |
|
| 96 | - } |
|
| 97 | - if(!$this->checkRequirements($reqs)) { |
|
| 98 | - throw new \Exception('Requirements not met', 400); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $attr = array('dn'); // default |
|
| 102 | - $limit = 1001; |
|
| 103 | - if($type === 'groups') { |
|
| 104 | - $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 105 | - } else if($type === 'users') { |
|
| 106 | - $result = $this->access->countUsers($filter, $attr, $limit); |
|
| 107 | - } else if ($type === 'objects') { |
|
| 108 | - $result = $this->access->countObjects($limit); |
|
| 109 | - } else { |
|
| 110 | - throw new \Exception('internal error: invalid object type', 500); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $result; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * formats the return value of a count operation to the string to be |
|
| 118 | - * inserted. |
|
| 119 | - * |
|
| 120 | - * @param bool|int $count |
|
| 121 | - * @return int|string |
|
| 122 | - */ |
|
| 123 | - private function formatCountResult($count) { |
|
| 124 | - $formatted = ($count !== false) ? $count : 0; |
|
| 125 | - if($formatted > 1000) { |
|
| 126 | - $formatted = '> 1000'; |
|
| 127 | - } |
|
| 128 | - return $formatted; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - public function countGroups() { |
|
| 132 | - $filter = $this->configuration->ldapGroupFilter; |
|
| 133 | - |
|
| 134 | - if(empty($filter)) { |
|
| 135 | - $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
| 136 | - $this->result->addChange('ldap_group_count', $output); |
|
| 137 | - return $this->result; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - try { |
|
| 141 | - $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
| 142 | - } catch (\Exception $e) { |
|
| 143 | - //400 can be ignored, 500 is forwarded |
|
| 144 | - if($e->getCode() === 500) { |
|
| 145 | - throw $e; |
|
| 146 | - } |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
| 150 | - $this->result->addChange('ldap_group_count', $output); |
|
| 151 | - return $this->result; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return WizardResult |
|
| 156 | - * @throws \Exception |
|
| 157 | - */ |
|
| 158 | - public function countUsers() { |
|
| 159 | - $filter = $this->access->getFilterForUserCount(); |
|
| 160 | - |
|
| 161 | - $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
| 162 | - $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
| 163 | - $this->result->addChange('ldap_user_count', $output); |
|
| 164 | - return $this->result; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * counts any objects in the currently set base dn |
|
| 169 | - * |
|
| 170 | - * @return WizardResult |
|
| 171 | - * @throws \Exception |
|
| 172 | - */ |
|
| 173 | - public function countInBaseDN() { |
|
| 174 | - // we don't need to provide a filter in this case |
|
| 175 | - $total = $this->countEntries(null, 'objects'); |
|
| 176 | - if($total === false) { |
|
| 177 | - throw new \Exception('invalid results received'); |
|
| 178 | - } |
|
| 179 | - $this->result->addChange('ldap_test_base', $total); |
|
| 180 | - return $this->result; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * counts users with a specified attribute |
|
| 185 | - * @param string $attr |
|
| 186 | - * @param bool $existsCheck |
|
| 187 | - * @return int|bool |
|
| 188 | - */ |
|
| 189 | - public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
| 190 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 191 | - 'ldapPort', |
|
| 192 | - 'ldapBase', |
|
| 193 | - 'ldapUserFilter', |
|
| 194 | - ))) { |
|
| 195 | - return false; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 199 | - $this->configuration->ldapUserFilter, |
|
| 200 | - $attr . '=*' |
|
| 201 | - )); |
|
| 202 | - |
|
| 203 | - $limit = ($existsCheck === false) ? null : 1; |
|
| 204 | - |
|
| 205 | - return $this->access->countUsers($filter, array('dn'), $limit); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * detects the display name attribute. If a setting is already present that |
|
| 210 | - * returns at least one hit, the detection will be canceled. |
|
| 211 | - * @return WizardResult|bool |
|
| 212 | - * @throws \Exception |
|
| 213 | - */ |
|
| 214 | - public function detectUserDisplayNameAttribute() { |
|
| 215 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 216 | - 'ldapPort', |
|
| 217 | - 'ldapBase', |
|
| 218 | - 'ldapUserFilter', |
|
| 219 | - ))) { |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $attr = $this->configuration->ldapUserDisplayName; |
|
| 224 | - if ($attr !== '' && $attr !== 'displayName') { |
|
| 225 | - // most likely not the default value with upper case N, |
|
| 226 | - // verify it still produces a result |
|
| 227 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 228 | - if($count > 0) { |
|
| 229 | - //no change, but we sent it back to make sure the user interface |
|
| 230 | - //is still correct, even if the ajax call was cancelled meanwhile |
|
| 231 | - $this->result->addChange('ldap_display_name', $attr); |
|
| 232 | - return $this->result; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - // first attribute that has at least one result wins |
|
| 237 | - $displayNameAttrs = array('displayname', 'cn'); |
|
| 238 | - foreach ($displayNameAttrs as $attr) { |
|
| 239 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 240 | - |
|
| 241 | - if($count > 0) { |
|
| 242 | - $this->applyFind('ldap_display_name', $attr); |
|
| 243 | - return $this->result; |
|
| 244 | - } |
|
| 245 | - }; |
|
| 246 | - |
|
| 247 | - throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.')); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * detects the most often used email attribute for users applying to the |
|
| 252 | - * user list filter. If a setting is already present that returns at least |
|
| 253 | - * one hit, the detection will be canceled. |
|
| 254 | - * @return WizardResult|bool |
|
| 255 | - */ |
|
| 256 | - public function detectEmailAttribute() { |
|
| 257 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 258 | - 'ldapPort', |
|
| 259 | - 'ldapBase', |
|
| 260 | - 'ldapUserFilter', |
|
| 261 | - ))) { |
|
| 262 | - return false; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $attr = $this->configuration->ldapEmailAttribute; |
|
| 266 | - if ($attr !== '') { |
|
| 267 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 268 | - if($count > 0) { |
|
| 269 | - return false; |
|
| 270 | - } |
|
| 271 | - $writeLog = true; |
|
| 272 | - } else { |
|
| 273 | - $writeLog = false; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
| 277 | - $winner = ''; |
|
| 278 | - $maxUsers = 0; |
|
| 279 | - foreach($emailAttributes as $attr) { |
|
| 280 | - $count = $this->countUsersWithAttribute($attr); |
|
| 281 | - if($count > $maxUsers) { |
|
| 282 | - $maxUsers = $count; |
|
| 283 | - $winner = $attr; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - if($winner !== '') { |
|
| 288 | - $this->applyFind('ldap_email_attr', $winner); |
|
| 289 | - if($writeLog) { |
|
| 290 | - \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 291 | - 'automatically been reset, because the original value ' . |
|
| 292 | - 'did not return any results.', \OCP\Util::INFO); |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - return $this->result; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @return WizardResult |
|
| 301 | - * @throws \Exception |
|
| 302 | - */ |
|
| 303 | - public function determineAttributes() { |
|
| 304 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 305 | - 'ldapPort', |
|
| 306 | - 'ldapBase', |
|
| 307 | - 'ldapUserFilter', |
|
| 308 | - ))) { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $attributes = $this->getUserAttributes(); |
|
| 313 | - |
|
| 314 | - natcasesort($attributes); |
|
| 315 | - $attributes = array_values($attributes); |
|
| 316 | - |
|
| 317 | - $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
| 318 | - |
|
| 319 | - $selected = $this->configuration->ldapLoginFilterAttributes; |
|
| 320 | - if(is_array($selected) && !empty($selected)) { |
|
| 321 | - $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - return $this->result; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * detects the available LDAP attributes |
|
| 329 | - * @return array|false The instance's WizardResult instance |
|
| 330 | - * @throws \Exception |
|
| 331 | - */ |
|
| 332 | - private function getUserAttributes() { |
|
| 333 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 334 | - 'ldapPort', |
|
| 335 | - 'ldapBase', |
|
| 336 | - 'ldapUserFilter', |
|
| 337 | - ))) { |
|
| 338 | - return false; |
|
| 339 | - } |
|
| 340 | - $cr = $this->getConnection(); |
|
| 341 | - if(!$cr) { |
|
| 342 | - throw new \Exception('Could not connect to LDAP'); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - $base = $this->configuration->ldapBase[0]; |
|
| 346 | - $filter = $this->configuration->ldapUserFilter; |
|
| 347 | - $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
| 348 | - if(!$this->ldap->isResource($rr)) { |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 352 | - $attributes = $this->ldap->getAttributes($cr, $er); |
|
| 353 | - $pureAttributes = array(); |
|
| 354 | - for($i = 0; $i < $attributes['count']; $i++) { |
|
| 355 | - $pureAttributes[] = $attributes[$i]; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - return $pureAttributes; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * detects the available LDAP groups |
|
| 363 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 364 | - */ |
|
| 365 | - public function determineGroupsForGroups() { |
|
| 366 | - return $this->determineGroups('ldap_groupfilter_groups', |
|
| 367 | - 'ldapGroupFilterGroups', |
|
| 368 | - false); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * detects the available LDAP groups |
|
| 373 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 374 | - */ |
|
| 375 | - public function determineGroupsForUsers() { |
|
| 376 | - return $this->determineGroups('ldap_userfilter_groups', |
|
| 377 | - 'ldapUserFilterGroups'); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * detects the available LDAP groups |
|
| 382 | - * @param string $dbKey |
|
| 383 | - * @param string $confKey |
|
| 384 | - * @param bool $testMemberOf |
|
| 385 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 386 | - * @throws \Exception |
|
| 387 | - */ |
|
| 388 | - private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
| 389 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 390 | - 'ldapPort', |
|
| 391 | - 'ldapBase', |
|
| 392 | - ))) { |
|
| 393 | - return false; |
|
| 394 | - } |
|
| 395 | - $cr = $this->getConnection(); |
|
| 396 | - if(!$cr) { |
|
| 397 | - throw new \Exception('Could not connect to LDAP'); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $this->fetchGroups($dbKey, $confKey); |
|
| 401 | - |
|
| 402 | - if($testMemberOf) { |
|
| 403 | - $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
| 404 | - $this->result->markChange(); |
|
| 405 | - if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 406 | - throw new \Exception('memberOf is not supported by the server'); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - return $this->result; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * fetches all groups from LDAP and adds them to the result object |
|
| 415 | - * |
|
| 416 | - * @param string $dbKey |
|
| 417 | - * @param string $confKey |
|
| 418 | - * @return array $groupEntries |
|
| 419 | - * @throws \Exception |
|
| 420 | - */ |
|
| 421 | - public function fetchGroups($dbKey, $confKey) { |
|
| 422 | - $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
|
| 423 | - |
|
| 424 | - $filterParts = array(); |
|
| 425 | - foreach($obclasses as $obclass) { |
|
| 426 | - $filterParts[] = 'objectclass='.$obclass; |
|
| 427 | - } |
|
| 428 | - //we filter for everything |
|
| 429 | - //- that looks like a group and |
|
| 430 | - //- has the group display name set |
|
| 431 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 432 | - $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
| 433 | - |
|
| 434 | - $groupNames = array(); |
|
| 435 | - $groupEntries = array(); |
|
| 436 | - $limit = 400; |
|
| 437 | - $offset = 0; |
|
| 438 | - do { |
|
| 439 | - // we need to request dn additionally here, otherwise memberOf |
|
| 440 | - // detection will fail later |
|
| 441 | - $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
| 442 | - foreach($result as $item) { |
|
| 443 | - if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 444 | - // just in case - no issue known |
|
| 445 | - continue; |
|
| 446 | - } |
|
| 447 | - $groupNames[] = $item['cn'][0]; |
|
| 448 | - $groupEntries[] = $item; |
|
| 449 | - } |
|
| 450 | - $offset += $limit; |
|
| 451 | - } while ($this->access->hasMoreResults()); |
|
| 452 | - |
|
| 453 | - if(count($groupNames) > 0) { |
|
| 454 | - natsort($groupNames); |
|
| 455 | - $this->result->addOptions($dbKey, array_values($groupNames)); |
|
| 456 | - } else { |
|
| 457 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $setFeatures = $this->configuration->$confKey; |
|
| 461 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 462 | - //something is already configured? pre-select it. |
|
| 463 | - $this->result->addChange($dbKey, $setFeatures); |
|
| 464 | - } |
|
| 465 | - return $groupEntries; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - public function determineGroupMemberAssoc() { |
|
| 469 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 470 | - 'ldapPort', |
|
| 471 | - 'ldapGroupFilter', |
|
| 472 | - ))) { |
|
| 473 | - return false; |
|
| 474 | - } |
|
| 475 | - $attribute = $this->detectGroupMemberAssoc(); |
|
| 476 | - if($attribute === false) { |
|
| 477 | - return false; |
|
| 478 | - } |
|
| 479 | - $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
| 480 | - $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
| 481 | - |
|
| 482 | - return $this->result; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Detects the available object classes |
|
| 487 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 488 | - * @throws \Exception |
|
| 489 | - */ |
|
| 490 | - public function determineGroupObjectClasses() { |
|
| 491 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 492 | - 'ldapPort', |
|
| 493 | - 'ldapBase', |
|
| 494 | - ))) { |
|
| 495 | - return false; |
|
| 496 | - } |
|
| 497 | - $cr = $this->getConnection(); |
|
| 498 | - if(!$cr) { |
|
| 499 | - throw new \Exception('Could not connect to LDAP'); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
| 503 | - $this->determineFeature($obclasses, |
|
| 504 | - 'objectclass', |
|
| 505 | - 'ldap_groupfilter_objectclass', |
|
| 506 | - 'ldapGroupFilterObjectclass', |
|
| 507 | - false); |
|
| 508 | - |
|
| 509 | - return $this->result; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * detects the available object classes |
|
| 514 | - * @return WizardResult |
|
| 515 | - * @throws \Exception |
|
| 516 | - */ |
|
| 517 | - public function determineUserObjectClasses() { |
|
| 518 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 519 | - 'ldapPort', |
|
| 520 | - 'ldapBase', |
|
| 521 | - ))) { |
|
| 522 | - return false; |
|
| 523 | - } |
|
| 524 | - $cr = $this->getConnection(); |
|
| 525 | - if(!$cr) { |
|
| 526 | - throw new \Exception('Could not connect to LDAP'); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
| 530 | - 'user', 'posixAccount', '*'); |
|
| 531 | - $filter = $this->configuration->ldapUserFilter; |
|
| 532 | - //if filter is empty, it is probably the first time the wizard is called |
|
| 533 | - //then, apply suggestions. |
|
| 534 | - $this->determineFeature($obclasses, |
|
| 535 | - 'objectclass', |
|
| 536 | - 'ldap_userfilter_objectclass', |
|
| 537 | - 'ldapUserFilterObjectclass', |
|
| 538 | - empty($filter)); |
|
| 539 | - |
|
| 540 | - return $this->result; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * @return WizardResult|false |
|
| 545 | - * @throws \Exception |
|
| 546 | - */ |
|
| 547 | - public function getGroupFilter() { |
|
| 548 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 549 | - 'ldapPort', |
|
| 550 | - 'ldapBase', |
|
| 551 | - ))) { |
|
| 552 | - return false; |
|
| 553 | - } |
|
| 554 | - //make sure the use display name is set |
|
| 555 | - $displayName = $this->configuration->ldapGroupDisplayName; |
|
| 556 | - if ($displayName === '') { |
|
| 557 | - $d = $this->configuration->getDefaults(); |
|
| 558 | - $this->applyFind('ldap_group_display_name', |
|
| 559 | - $d['ldap_group_display_name']); |
|
| 560 | - } |
|
| 561 | - $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
| 562 | - |
|
| 563 | - $this->applyFind('ldap_group_filter', $filter); |
|
| 564 | - return $this->result; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * @return WizardResult|false |
|
| 569 | - * @throws \Exception |
|
| 570 | - */ |
|
| 571 | - public function getUserListFilter() { |
|
| 572 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 573 | - 'ldapPort', |
|
| 574 | - 'ldapBase', |
|
| 575 | - ))) { |
|
| 576 | - return false; |
|
| 577 | - } |
|
| 578 | - //make sure the use display name is set |
|
| 579 | - $displayName = $this->configuration->ldapUserDisplayName; |
|
| 580 | - if ($displayName === '') { |
|
| 581 | - $d = $this->configuration->getDefaults(); |
|
| 582 | - $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
| 583 | - } |
|
| 584 | - $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
| 585 | - if(!$filter) { |
|
| 586 | - throw new \Exception('Cannot create filter'); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $this->applyFind('ldap_userlist_filter', $filter); |
|
| 590 | - return $this->result; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * @return bool|WizardResult |
|
| 595 | - * @throws \Exception |
|
| 596 | - */ |
|
| 597 | - public function getUserLoginFilter() { |
|
| 598 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 599 | - 'ldapPort', |
|
| 600 | - 'ldapBase', |
|
| 601 | - 'ldapUserFilter', |
|
| 602 | - ))) { |
|
| 603 | - return false; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
| 607 | - if(!$filter) { |
|
| 608 | - throw new \Exception('Cannot create filter'); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - $this->applyFind('ldap_login_filter', $filter); |
|
| 612 | - return $this->result; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * @return bool|WizardResult |
|
| 617 | - * @param string $loginName |
|
| 618 | - * @throws \Exception |
|
| 619 | - */ |
|
| 620 | - public function testLoginName($loginName) { |
|
| 621 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 622 | - 'ldapPort', |
|
| 623 | - 'ldapBase', |
|
| 624 | - 'ldapLoginFilter', |
|
| 625 | - ))) { |
|
| 626 | - return false; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - $cr = $this->access->connection->getConnectionResource(); |
|
| 630 | - if(!$this->ldap->isResource($cr)) { |
|
| 631 | - throw new \Exception('connection error'); |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 635 | - === false) { |
|
| 636 | - throw new \Exception('missing placeholder'); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - $users = $this->access->countUsersByLoginName($loginName); |
|
| 640 | - if($this->ldap->errno($cr) !== 0) { |
|
| 641 | - throw new \Exception($this->ldap->error($cr)); |
|
| 642 | - } |
|
| 643 | - $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
| 644 | - $this->result->addChange('ldap_test_loginname', $users); |
|
| 645 | - $this->result->addChange('ldap_test_effective_filter', $filter); |
|
| 646 | - return $this->result; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Tries to determine the port, requires given Host, User DN and Password |
|
| 651 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
| 652 | - * @throws \Exception |
|
| 653 | - */ |
|
| 654 | - public function guessPortAndTLS() { |
|
| 655 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 656 | - ))) { |
|
| 657 | - return false; |
|
| 658 | - } |
|
| 659 | - $this->checkHost(); |
|
| 660 | - $portSettings = $this->getPortSettingsToTry(); |
|
| 661 | - |
|
| 662 | - if(!is_array($portSettings)) { |
|
| 663 | - throw new \Exception(print_r($portSettings, true)); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - //proceed from the best configuration and return on first success |
|
| 667 | - foreach($portSettings as $setting) { |
|
| 668 | - $p = $setting['port']; |
|
| 669 | - $t = $setting['tls']; |
|
| 670 | - \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 671 | - //connectAndBind may throw Exception, it needs to be catched by the |
|
| 672 | - //callee of this method |
|
| 673 | - |
|
| 674 | - try { |
|
| 675 | - $settingsFound = $this->connectAndBind($p, $t); |
|
| 676 | - } catch (\Exception $e) { |
|
| 677 | - // any reply other than -1 (= cannot connect) is already okay, |
|
| 678 | - // because then we found the server |
|
| 679 | - // unavailable startTLS returns -11 |
|
| 680 | - if($e->getCode() > 0) { |
|
| 681 | - $settingsFound = true; |
|
| 682 | - } else { |
|
| 683 | - throw $e; |
|
| 684 | - } |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - if ($settingsFound === true) { |
|
| 688 | - $config = array( |
|
| 689 | - 'ldapPort' => $p, |
|
| 690 | - 'ldapTLS' => intval($t) |
|
| 691 | - ); |
|
| 692 | - $this->configuration->setConfiguration($config); |
|
| 693 | - \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 694 | - $this->result->addChange('ldap_port', $p); |
|
| 695 | - return $this->result; |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - //custom port, undetected (we do not brute force) |
|
| 700 | - return false; |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * tries to determine a base dn from User DN or LDAP Host |
|
| 705 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
| 706 | - */ |
|
| 707 | - public function guessBaseDN() { |
|
| 708 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 709 | - 'ldapPort', |
|
| 710 | - ))) { |
|
| 711 | - return false; |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - //check whether a DN is given in the agent name (99.9% of all cases) |
|
| 715 | - $base = null; |
|
| 716 | - $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
| 717 | - if($i !== false) { |
|
| 718 | - $base = substr($this->configuration->ldapAgentName, $i); |
|
| 719 | - if($this->testBaseDN($base)) { |
|
| 720 | - $this->applyFind('ldap_base', $base); |
|
| 721 | - return $this->result; |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - //this did not help :( |
|
| 726 | - //Let's see whether we can parse the Host URL and convert the domain to |
|
| 727 | - //a base DN |
|
| 728 | - $helper = new Helper(\OC::$server->getConfig()); |
|
| 729 | - $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
| 730 | - if(!$domain) { |
|
| 731 | - return false; |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - $dparts = explode('.', $domain); |
|
| 735 | - while(count($dparts) > 0) { |
|
| 736 | - $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 737 | - if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
| 738 | - $this->applyFind('ldap_base', $base2); |
|
| 739 | - return $this->result; |
|
| 740 | - } |
|
| 741 | - array_shift($dparts); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - return false; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * sets the found value for the configuration key in the WizardResult |
|
| 749 | - * as well as in the Configuration instance |
|
| 750 | - * @param string $key the configuration key |
|
| 751 | - * @param string $value the (detected) value |
|
| 752 | - * |
|
| 753 | - */ |
|
| 754 | - private function applyFind($key, $value) { |
|
| 755 | - $this->result->addChange($key, $value); |
|
| 756 | - $this->configuration->setConfiguration(array($key => $value)); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * Checks, whether a port was entered in the Host configuration |
|
| 761 | - * field. In this case the port will be stripped off, but also stored as |
|
| 762 | - * setting. |
|
| 763 | - */ |
|
| 764 | - private function checkHost() { |
|
| 765 | - $host = $this->configuration->ldapHost; |
|
| 766 | - $hostInfo = parse_url($host); |
|
| 767 | - |
|
| 768 | - //removes Port from Host |
|
| 769 | - if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 770 | - $port = $hostInfo['port']; |
|
| 771 | - $host = str_replace(':'.$port, '', $host); |
|
| 772 | - $this->applyFind('ldap_host', $host); |
|
| 773 | - $this->applyFind('ldap_port', $port); |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * tries to detect the group member association attribute which is |
|
| 779 | - * one of 'uniqueMember', 'memberUid', 'member', 'gidNumber' |
|
| 780 | - * @return string|false, string with the attribute name, false on error |
|
| 781 | - * @throws \Exception |
|
| 782 | - */ |
|
| 783 | - private function detectGroupMemberAssoc() { |
|
| 784 | - $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
|
| 785 | - $filter = $this->configuration->ldapGroupFilter; |
|
| 786 | - if(empty($filter)) { |
|
| 787 | - return false; |
|
| 788 | - } |
|
| 789 | - $cr = $this->getConnection(); |
|
| 790 | - if(!$cr) { |
|
| 791 | - throw new \Exception('Could not connect to LDAP'); |
|
| 792 | - } |
|
| 793 | - $base = $this->configuration->ldapBase[0]; |
|
| 794 | - $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
| 795 | - if(!$this->ldap->isResource($rr)) { |
|
| 796 | - return false; |
|
| 797 | - } |
|
| 798 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 799 | - while(is_resource($er)) { |
|
| 800 | - $this->ldap->getDN($cr, $er); |
|
| 801 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 802 | - $result = array(); |
|
| 803 | - $possibleAttrsCount = count($possibleAttrs); |
|
| 804 | - for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 805 | - if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 806 | - $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - if(!empty($result)) { |
|
| 810 | - natsort($result); |
|
| 811 | - return key($result); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - $er = $this->ldap->nextEntry($cr, $er); |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - return false; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Checks whether for a given BaseDN results will be returned |
|
| 822 | - * @param string $base the BaseDN to test |
|
| 823 | - * @return bool true on success, false otherwise |
|
| 824 | - * @throws \Exception |
|
| 825 | - */ |
|
| 826 | - private function testBaseDN($base) { |
|
| 827 | - $cr = $this->getConnection(); |
|
| 828 | - if(!$cr) { |
|
| 829 | - throw new \Exception('Could not connect to LDAP'); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - //base is there, let's validate it. If we search for anything, we should |
|
| 833 | - //get a result set > 0 on a proper base |
|
| 834 | - $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
| 835 | - if(!$this->ldap->isResource($rr)) { |
|
| 836 | - $errorNo = $this->ldap->errno($cr); |
|
| 837 | - $errorMsg = $this->ldap->error($cr); |
|
| 838 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
| 839 | - ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
| 840 | - return false; |
|
| 841 | - } |
|
| 842 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
| 843 | - return ($entries !== false) && ($entries > 0); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Checks whether the server supports memberOf in LDAP Filter. |
|
| 848 | - * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
| 849 | - * a configured objectClass. I.e. not necessarily for all available groups |
|
| 850 | - * memberOf does work. |
|
| 851 | - * |
|
| 852 | - * @return bool true if it does, false otherwise |
|
| 853 | - * @throws \Exception |
|
| 854 | - */ |
|
| 855 | - private function testMemberOf() { |
|
| 856 | - $cr = $this->getConnection(); |
|
| 857 | - if(!$cr) { |
|
| 858 | - throw new \Exception('Could not connect to LDAP'); |
|
| 859 | - } |
|
| 860 | - $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
| 861 | - if(is_int($result) && $result > 0) { |
|
| 862 | - return true; |
|
| 863 | - } |
|
| 864 | - return false; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - /** |
|
| 868 | - * creates an LDAP Filter from given configuration |
|
| 869 | - * @param integer $filterType int, for which use case the filter shall be created |
|
| 870 | - * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
| 871 | - * self::LFILTER_GROUP_LIST |
|
| 872 | - * @return string|false string with the filter on success, false otherwise |
|
| 873 | - * @throws \Exception |
|
| 874 | - */ |
|
| 875 | - private function composeLdapFilter($filterType) { |
|
| 876 | - $filter = ''; |
|
| 877 | - $parts = 0; |
|
| 878 | - switch ($filterType) { |
|
| 879 | - case self::LFILTER_USER_LIST: |
|
| 880 | - $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
| 881 | - //glue objectclasses |
|
| 882 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 883 | - $filter .= '(|'; |
|
| 884 | - foreach($objcs as $objc) { |
|
| 885 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 886 | - } |
|
| 887 | - $filter .= ')'; |
|
| 888 | - $parts++; |
|
| 889 | - } |
|
| 890 | - //glue group memberships |
|
| 891 | - if($this->configuration->hasMemberOfFilterSupport) { |
|
| 892 | - $cns = $this->configuration->ldapUserFilterGroups; |
|
| 893 | - if(is_array($cns) && count($cns) > 0) { |
|
| 894 | - $filter .= '(|'; |
|
| 895 | - $cr = $this->getConnection(); |
|
| 896 | - if(!$cr) { |
|
| 897 | - throw new \Exception('Could not connect to LDAP'); |
|
| 898 | - } |
|
| 899 | - $base = $this->configuration->ldapBase[0]; |
|
| 900 | - foreach($cns as $cn) { |
|
| 901 | - $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 902 | - if(!$this->ldap->isResource($rr)) { |
|
| 903 | - continue; |
|
| 904 | - } |
|
| 905 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 906 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 907 | - $dn = $this->ldap->getDN($cr, $er); |
|
| 908 | - if ($dn == false || $dn === '') { |
|
| 909 | - continue; |
|
| 910 | - } |
|
| 911 | - $filterPart = '(memberof=' . $dn . ')'; |
|
| 912 | - if(isset($attrs['primaryGroupToken'])) { |
|
| 913 | - $pgt = $attrs['primaryGroupToken'][0]; |
|
| 914 | - $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 915 | - $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 916 | - } |
|
| 917 | - $filter .= $filterPart; |
|
| 918 | - } |
|
| 919 | - $filter .= ')'; |
|
| 920 | - } |
|
| 921 | - $parts++; |
|
| 922 | - } |
|
| 923 | - //wrap parts in AND condition |
|
| 924 | - if($parts > 1) { |
|
| 925 | - $filter = '(&' . $filter . ')'; |
|
| 926 | - } |
|
| 927 | - if ($filter === '') { |
|
| 928 | - $filter = '(objectclass=*)'; |
|
| 929 | - } |
|
| 930 | - break; |
|
| 931 | - |
|
| 932 | - case self::LFILTER_GROUP_LIST: |
|
| 933 | - $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
| 934 | - //glue objectclasses |
|
| 935 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 936 | - $filter .= '(|'; |
|
| 937 | - foreach($objcs as $objc) { |
|
| 938 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 939 | - } |
|
| 940 | - $filter .= ')'; |
|
| 941 | - $parts++; |
|
| 942 | - } |
|
| 943 | - //glue group memberships |
|
| 944 | - $cns = $this->configuration->ldapGroupFilterGroups; |
|
| 945 | - if(is_array($cns) && count($cns) > 0) { |
|
| 946 | - $filter .= '(|'; |
|
| 947 | - foreach($cns as $cn) { |
|
| 948 | - $filter .= '(cn=' . $cn . ')'; |
|
| 949 | - } |
|
| 950 | - $filter .= ')'; |
|
| 951 | - } |
|
| 952 | - $parts++; |
|
| 953 | - //wrap parts in AND condition |
|
| 954 | - if($parts > 1) { |
|
| 955 | - $filter = '(&' . $filter . ')'; |
|
| 956 | - } |
|
| 957 | - break; |
|
| 958 | - |
|
| 959 | - case self::LFILTER_LOGIN: |
|
| 960 | - $ulf = $this->configuration->ldapUserFilter; |
|
| 961 | - $loginpart = '=%uid'; |
|
| 962 | - $filterUsername = ''; |
|
| 963 | - $userAttributes = $this->getUserAttributes(); |
|
| 964 | - $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
| 965 | - $parts = 0; |
|
| 966 | - |
|
| 967 | - if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 968 | - $attr = ''; |
|
| 969 | - if(isset($userAttributes['uid'])) { |
|
| 970 | - $attr = 'uid'; |
|
| 971 | - } else if(isset($userAttributes['samaccountname'])) { |
|
| 972 | - $attr = 'samaccountname'; |
|
| 973 | - } else if(isset($userAttributes['cn'])) { |
|
| 974 | - //fallback |
|
| 975 | - $attr = 'cn'; |
|
| 976 | - } |
|
| 977 | - if ($attr !== '') { |
|
| 978 | - $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 979 | - $parts++; |
|
| 980 | - } |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - $filterEmail = ''; |
|
| 984 | - if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 985 | - $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
| 986 | - $parts++; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - $filterAttributes = ''; |
|
| 990 | - $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
| 991 | - if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 992 | - $filterAttributes = '(|'; |
|
| 993 | - foreach($attrsToFilter as $attribute) { |
|
| 994 | - $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 995 | - } |
|
| 996 | - $filterAttributes .= ')'; |
|
| 997 | - $parts++; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - $filterLogin = ''; |
|
| 1001 | - if($parts > 1) { |
|
| 1002 | - $filterLogin = '(|'; |
|
| 1003 | - } |
|
| 1004 | - $filterLogin .= $filterUsername; |
|
| 1005 | - $filterLogin .= $filterEmail; |
|
| 1006 | - $filterLogin .= $filterAttributes; |
|
| 1007 | - if($parts > 1) { |
|
| 1008 | - $filterLogin .= ')'; |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - $filter = '(&'.$ulf.$filterLogin.')'; |
|
| 1012 | - break; |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
| 1016 | - |
|
| 1017 | - return $filter; |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - /** |
|
| 1021 | - * Connects and Binds to an LDAP Server |
|
| 1022 | - * @param int $port the port to connect with |
|
| 1023 | - * @param bool $tls whether startTLS is to be used |
|
| 1024 | - * @param bool $ncc no certificate check |
|
| 1025 | - * @return bool |
|
| 1026 | - * @throws \Exception |
|
| 1027 | - */ |
|
| 1028 | - private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
|
| 1029 | - if($ncc) { |
|
| 1030 | - $originalLDAPTLS_REQCERT = strval(getenv('LDAPTLS_REQCERT')); |
|
| 1031 | - //No certificate check |
|
| 1032 | - putenv('LDAPTLS_REQCERT=never'); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - //connect, does not really trigger any server communication |
|
| 1036 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
|
| 1037 | - $host = $this->configuration->ldapHost; |
|
| 1038 | - $hostInfo = parse_url($host); |
|
| 1039 | - if(!$hostInfo) { |
|
| 1040 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1041 | - } |
|
| 1042 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
| 1043 | - $cr = $this->ldap->connect($host, $port); |
|
| 1044 | - if(!is_resource($cr)) { |
|
| 1045 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1046 | - } |
|
| 1047 | - |
|
| 1048 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Setting LDAP Options ', \OCP\Util::DEBUG); |
|
| 1049 | - //set LDAP options |
|
| 1050 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1051 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1052 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1053 | - |
|
| 1054 | - try { |
|
| 1055 | - if($tls) { |
|
| 1056 | - $isTlsWorking = @$this->ldap->startTls($cr); |
|
| 1057 | - if(!$isTlsWorking) { |
|
| 1058 | - return false; |
|
| 1059 | - } |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
| 1063 | - //interesting part: do the bind! |
|
| 1064 | - $login = $this->ldap->bind($cr, |
|
| 1065 | - $this->configuration->ldapAgentName, |
|
| 1066 | - $this->configuration->ldapAgentPassword |
|
| 1067 | - ); |
|
| 1068 | - $errNo = $this->ldap->errno($cr); |
|
| 1069 | - $error = ldap_error($cr); |
|
| 1070 | - $this->ldap->unbind($cr); |
|
| 1071 | - } catch(ServerNotAvailableException $e) { |
|
| 1072 | - if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1073 | - putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1074 | - } |
|
| 1075 | - return false; |
|
| 1076 | - } |
|
| 1077 | - |
|
| 1078 | - if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1079 | - putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1080 | - } |
|
| 1081 | - |
|
| 1082 | - if($login === true) { |
|
| 1083 | - $this->ldap->unbind($cr); |
|
| 1084 | - if($ncc) { |
|
| 1085 | - throw new \Exception('Certificate cannot be validated.'); |
|
| 1086 | - } |
|
| 1087 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
|
| 1088 | - return true; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - if($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
| 1092 | - //host, port or TLS wrong |
|
| 1093 | - return false; |
|
| 1094 | - } else if ($errNo === 2) { |
|
| 1095 | - return $this->connectAndBind($port, $tls, true); |
|
| 1096 | - } |
|
| 1097 | - throw new \Exception($error, $errNo); |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - /** |
|
| 1101 | - * checks whether a valid combination of agent and password has been |
|
| 1102 | - * provided (either two values or nothing for anonymous connect) |
|
| 1103 | - * @return bool, true if everything is fine, false otherwise |
|
| 1104 | - */ |
|
| 1105 | - private function checkAgentRequirements() { |
|
| 1106 | - $agent = $this->configuration->ldapAgentName; |
|
| 1107 | - $pwd = $this->configuration->ldapAgentPassword; |
|
| 1108 | - |
|
| 1109 | - return |
|
| 1110 | - ($agent !== '' && $pwd !== '') |
|
| 1111 | - || ($agent === '' && $pwd === '') |
|
| 1112 | - ; |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * @param array $reqs |
|
| 1117 | - * @return bool |
|
| 1118 | - */ |
|
| 1119 | - private function checkRequirements($reqs) { |
|
| 1120 | - $this->checkAgentRequirements(); |
|
| 1121 | - foreach($reqs as $option) { |
|
| 1122 | - $value = $this->configuration->$option; |
|
| 1123 | - if(empty($value)) { |
|
| 1124 | - return false; |
|
| 1125 | - } |
|
| 1126 | - } |
|
| 1127 | - return true; |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - /** |
|
| 1131 | - * does a cumulativeSearch on LDAP to get different values of a |
|
| 1132 | - * specified attribute |
|
| 1133 | - * @param string[] $filters array, the filters that shall be used in the search |
|
| 1134 | - * @param string $attr the attribute of which a list of values shall be returned |
|
| 1135 | - * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
| 1136 | - * The lower, the faster |
|
| 1137 | - * @param string $maxF string. if not null, this variable will have the filter that |
|
| 1138 | - * yields most result entries |
|
| 1139 | - * @return array|false an array with the values on success, false otherwise |
|
| 1140 | - */ |
|
| 1141 | - public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
| 1142 | - $dnRead = array(); |
|
| 1143 | - $foundItems = array(); |
|
| 1144 | - $maxEntries = 0; |
|
| 1145 | - if(!is_array($this->configuration->ldapBase) |
|
| 1146 | - || !isset($this->configuration->ldapBase[0])) { |
|
| 1147 | - return false; |
|
| 1148 | - } |
|
| 1149 | - $base = $this->configuration->ldapBase[0]; |
|
| 1150 | - $cr = $this->getConnection(); |
|
| 1151 | - if(!$this->ldap->isResource($cr)) { |
|
| 1152 | - return false; |
|
| 1153 | - } |
|
| 1154 | - $lastFilter = null; |
|
| 1155 | - if(isset($filters[count($filters)-1])) { |
|
| 1156 | - $lastFilter = $filters[count($filters)-1]; |
|
| 1157 | - } |
|
| 1158 | - foreach($filters as $filter) { |
|
| 1159 | - if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1160 | - //skip when the filter is a wildcard and results were found |
|
| 1161 | - continue; |
|
| 1162 | - } |
|
| 1163 | - // 20k limit for performance and reason |
|
| 1164 | - $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
| 1165 | - if(!$this->ldap->isResource($rr)) { |
|
| 1166 | - continue; |
|
| 1167 | - } |
|
| 1168 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
| 1169 | - $getEntryFunc = 'firstEntry'; |
|
| 1170 | - if(($entries !== false) && ($entries > 0)) { |
|
| 1171 | - if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1172 | - $maxEntries = $entries; |
|
| 1173 | - $maxF = $filter; |
|
| 1174 | - } |
|
| 1175 | - $dnReadCount = 0; |
|
| 1176 | - do { |
|
| 1177 | - $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
| 1178 | - $getEntryFunc = 'nextEntry'; |
|
| 1179 | - if(!$this->ldap->isResource($entry)) { |
|
| 1180 | - continue 2; |
|
| 1181 | - } |
|
| 1182 | - $rr = $entry; //will be expected by nextEntry next round |
|
| 1183 | - $attributes = $this->ldap->getAttributes($cr, $entry); |
|
| 1184 | - $dn = $this->ldap->getDN($cr, $entry); |
|
| 1185 | - if($dn === false || in_array($dn, $dnRead)) { |
|
| 1186 | - continue; |
|
| 1187 | - } |
|
| 1188 | - $newItems = array(); |
|
| 1189 | - $state = $this->getAttributeValuesFromEntry($attributes, |
|
| 1190 | - $attr, |
|
| 1191 | - $newItems); |
|
| 1192 | - $dnReadCount++; |
|
| 1193 | - $foundItems = array_merge($foundItems, $newItems); |
|
| 1194 | - $this->resultCache[$dn][$attr] = $newItems; |
|
| 1195 | - $dnRead[] = $dn; |
|
| 1196 | - } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1197 | - || $this->ldap->isResource($entry)) |
|
| 1198 | - && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
| 1199 | - } |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - return array_unique($foundItems); |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * determines if and which $attr are available on the LDAP server |
|
| 1207 | - * @param string[] $objectclasses the objectclasses to use as search filter |
|
| 1208 | - * @param string $attr the attribute to look for |
|
| 1209 | - * @param string $dbkey the dbkey of the setting the feature is connected to |
|
| 1210 | - * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
| 1211 | - * Configuration class |
|
| 1212 | - * @param bool $po whether the objectClass with most result entries |
|
| 1213 | - * shall be pre-selected via the result |
|
| 1214 | - * @return array|false list of found items. |
|
| 1215 | - * @throws \Exception |
|
| 1216 | - */ |
|
| 1217 | - private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
| 1218 | - $cr = $this->getConnection(); |
|
| 1219 | - if(!$cr) { |
|
| 1220 | - throw new \Exception('Could not connect to LDAP'); |
|
| 1221 | - } |
|
| 1222 | - $p = 'objectclass='; |
|
| 1223 | - foreach($objectclasses as $key => $value) { |
|
| 1224 | - $objectclasses[$key] = $p.$value; |
|
| 1225 | - } |
|
| 1226 | - $maxEntryObjC = ''; |
|
| 1227 | - |
|
| 1228 | - //how deep to dig? |
|
| 1229 | - //When looking for objectclasses, testing few entries is sufficient, |
|
| 1230 | - $dig = 3; |
|
| 1231 | - |
|
| 1232 | - $availableFeatures = |
|
| 1233 | - $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
| 1234 | - $dig, $maxEntryObjC); |
|
| 1235 | - if(is_array($availableFeatures) |
|
| 1236 | - && count($availableFeatures) > 0) { |
|
| 1237 | - natcasesort($availableFeatures); |
|
| 1238 | - //natcasesort keeps indices, but we must get rid of them for proper |
|
| 1239 | - //sorting in the web UI. Therefore: array_values |
|
| 1240 | - $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
| 1241 | - } else { |
|
| 1242 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - $setFeatures = $this->configuration->$confkey; |
|
| 1246 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1247 | - //something is already configured? pre-select it. |
|
| 1248 | - $this->result->addChange($dbkey, $setFeatures); |
|
| 1249 | - } else if ($po && $maxEntryObjC !== '') { |
|
| 1250 | - //pre-select objectclass with most result entries |
|
| 1251 | - $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
| 1252 | - $this->applyFind($dbkey, $maxEntryObjC); |
|
| 1253 | - $this->result->addChange($dbkey, $maxEntryObjC); |
|
| 1254 | - } |
|
| 1255 | - |
|
| 1256 | - return $availableFeatures; |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - /** |
|
| 1260 | - * appends a list of values fr |
|
| 1261 | - * @param resource $result the return value from ldap_get_attributes |
|
| 1262 | - * @param string $attribute the attribute values to look for |
|
| 1263 | - * @param array &$known new values will be appended here |
|
| 1264 | - * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
| 1265 | - * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
| 1266 | - */ |
|
| 1267 | - private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
| 1268 | - if(!is_array($result) |
|
| 1269 | - || !isset($result['count']) |
|
| 1270 | - || !$result['count'] > 0) { |
|
| 1271 | - return self::LRESULT_PROCESSED_INVALID; |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - // strtolower on all keys for proper comparison |
|
| 1275 | - $result = \OCP\Util::mb_array_change_key_case($result); |
|
| 1276 | - $attribute = strtolower($attribute); |
|
| 1277 | - if(isset($result[$attribute])) { |
|
| 1278 | - foreach($result[$attribute] as $key => $val) { |
|
| 1279 | - if($key === 'count') { |
|
| 1280 | - continue; |
|
| 1281 | - } |
|
| 1282 | - if(!in_array($val, $known)) { |
|
| 1283 | - $known[] = $val; |
|
| 1284 | - } |
|
| 1285 | - } |
|
| 1286 | - return self::LRESULT_PROCESSED_OK; |
|
| 1287 | - } else { |
|
| 1288 | - return self::LRESULT_PROCESSED_SKIP; |
|
| 1289 | - } |
|
| 1290 | - } |
|
| 1291 | - |
|
| 1292 | - /** |
|
| 1293 | - * @return bool|mixed |
|
| 1294 | - */ |
|
| 1295 | - private function getConnection() { |
|
| 1296 | - if(!is_null($this->cr)) { |
|
| 1297 | - return $this->cr; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - $cr = $this->ldap->connect( |
|
| 1301 | - $this->configuration->ldapHost, |
|
| 1302 | - $this->configuration->ldapPort |
|
| 1303 | - ); |
|
| 1304 | - |
|
| 1305 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1306 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1307 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1308 | - if($this->configuration->ldapTLS === 1) { |
|
| 1309 | - $this->ldap->startTls($cr); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - $lo = @$this->ldap->bind($cr, |
|
| 1313 | - $this->configuration->ldapAgentName, |
|
| 1314 | - $this->configuration->ldapAgentPassword); |
|
| 1315 | - if($lo === true) { |
|
| 1316 | - $this->$cr = $cr; |
|
| 1317 | - return $cr; |
|
| 1318 | - } |
|
| 1319 | - |
|
| 1320 | - return false; |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - /** |
|
| 1324 | - * @return array |
|
| 1325 | - */ |
|
| 1326 | - private function getDefaultLdapPortSettings() { |
|
| 1327 | - static $settings = array( |
|
| 1328 | - array('port' => 7636, 'tls' => false), |
|
| 1329 | - array('port' => 636, 'tls' => false), |
|
| 1330 | - array('port' => 7389, 'tls' => true), |
|
| 1331 | - array('port' => 389, 'tls' => true), |
|
| 1332 | - array('port' => 7389, 'tls' => false), |
|
| 1333 | - array('port' => 389, 'tls' => false), |
|
| 1334 | - ); |
|
| 1335 | - return $settings; |
|
| 1336 | - } |
|
| 1337 | - |
|
| 1338 | - /** |
|
| 1339 | - * @return array |
|
| 1340 | - */ |
|
| 1341 | - private function getPortSettingsToTry() { |
|
| 1342 | - //389 ← LDAP / Unencrypted or StartTLS |
|
| 1343 | - //636 ← LDAPS / SSL |
|
| 1344 | - //7xxx ← UCS. need to be checked first, because both ports may be open |
|
| 1345 | - $host = $this->configuration->ldapHost; |
|
| 1346 | - $port = intval($this->configuration->ldapPort); |
|
| 1347 | - $portSettings = array(); |
|
| 1348 | - |
|
| 1349 | - //In case the port is already provided, we will check this first |
|
| 1350 | - if($port > 0) { |
|
| 1351 | - $hostInfo = parse_url($host); |
|
| 1352 | - if(!(is_array($hostInfo) |
|
| 1353 | - && isset($hostInfo['scheme']) |
|
| 1354 | - && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
| 1355 | - $portSettings[] = array('port' => $port, 'tls' => true); |
|
| 1356 | - } |
|
| 1357 | - $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1358 | - } |
|
| 1359 | - |
|
| 1360 | - //default ports |
|
| 1361 | - $portSettings = array_merge($portSettings, |
|
| 1362 | - $this->getDefaultLdapPortSettings()); |
|
| 1363 | - |
|
| 1364 | - return $portSettings; |
|
| 1365 | - } |
|
| 41 | + /** @var \OCP\IL10N */ |
|
| 42 | + static protected $l; |
|
| 43 | + protected $access; |
|
| 44 | + protected $cr; |
|
| 45 | + protected $configuration; |
|
| 46 | + protected $result; |
|
| 47 | + protected $resultCache = array(); |
|
| 48 | + |
|
| 49 | + const LRESULT_PROCESSED_OK = 2; |
|
| 50 | + const LRESULT_PROCESSED_INVALID = 3; |
|
| 51 | + const LRESULT_PROCESSED_SKIP = 4; |
|
| 52 | + |
|
| 53 | + const LFILTER_LOGIN = 2; |
|
| 54 | + const LFILTER_USER_LIST = 3; |
|
| 55 | + const LFILTER_GROUP_LIST = 4; |
|
| 56 | + |
|
| 57 | + const LFILTER_MODE_ASSISTED = 2; |
|
| 58 | + const LFILTER_MODE_RAW = 1; |
|
| 59 | + |
|
| 60 | + const LDAP_NW_TIMEOUT = 4; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Constructor |
|
| 64 | + * @param Configuration $configuration an instance of Configuration |
|
| 65 | + * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
| 66 | + * @param Access $access |
|
| 67 | + */ |
|
| 68 | + public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
| 69 | + parent::__construct($ldap); |
|
| 70 | + $this->configuration = $configuration; |
|
| 71 | + if(is_null(Wizard::$l)) { |
|
| 72 | + Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
| 73 | + } |
|
| 74 | + $this->access = $access; |
|
| 75 | + $this->result = new WizardResult(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function __destruct() { |
|
| 79 | + if($this->result->hasChanges()) { |
|
| 80 | + $this->configuration->saveConfiguration(); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * counts entries in the LDAP directory |
|
| 86 | + * |
|
| 87 | + * @param string $filter the LDAP search filter |
|
| 88 | + * @param string $type a string being either 'users' or 'groups'; |
|
| 89 | + * @return bool|int |
|
| 90 | + * @throws \Exception |
|
| 91 | + */ |
|
| 92 | + public function countEntries($filter, $type) { |
|
| 93 | + $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
| 94 | + if($type === 'users') { |
|
| 95 | + $reqs[] = 'ldapUserFilter'; |
|
| 96 | + } |
|
| 97 | + if(!$this->checkRequirements($reqs)) { |
|
| 98 | + throw new \Exception('Requirements not met', 400); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $attr = array('dn'); // default |
|
| 102 | + $limit = 1001; |
|
| 103 | + if($type === 'groups') { |
|
| 104 | + $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 105 | + } else if($type === 'users') { |
|
| 106 | + $result = $this->access->countUsers($filter, $attr, $limit); |
|
| 107 | + } else if ($type === 'objects') { |
|
| 108 | + $result = $this->access->countObjects($limit); |
|
| 109 | + } else { |
|
| 110 | + throw new \Exception('internal error: invalid object type', 500); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $result; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * formats the return value of a count operation to the string to be |
|
| 118 | + * inserted. |
|
| 119 | + * |
|
| 120 | + * @param bool|int $count |
|
| 121 | + * @return int|string |
|
| 122 | + */ |
|
| 123 | + private function formatCountResult($count) { |
|
| 124 | + $formatted = ($count !== false) ? $count : 0; |
|
| 125 | + if($formatted > 1000) { |
|
| 126 | + $formatted = '> 1000'; |
|
| 127 | + } |
|
| 128 | + return $formatted; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + public function countGroups() { |
|
| 132 | + $filter = $this->configuration->ldapGroupFilter; |
|
| 133 | + |
|
| 134 | + if(empty($filter)) { |
|
| 135 | + $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
| 136 | + $this->result->addChange('ldap_group_count', $output); |
|
| 137 | + return $this->result; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + try { |
|
| 141 | + $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
| 142 | + } catch (\Exception $e) { |
|
| 143 | + //400 can be ignored, 500 is forwarded |
|
| 144 | + if($e->getCode() === 500) { |
|
| 145 | + throw $e; |
|
| 146 | + } |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
| 150 | + $this->result->addChange('ldap_group_count', $output); |
|
| 151 | + return $this->result; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return WizardResult |
|
| 156 | + * @throws \Exception |
|
| 157 | + */ |
|
| 158 | + public function countUsers() { |
|
| 159 | + $filter = $this->access->getFilterForUserCount(); |
|
| 160 | + |
|
| 161 | + $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
| 162 | + $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
| 163 | + $this->result->addChange('ldap_user_count', $output); |
|
| 164 | + return $this->result; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * counts any objects in the currently set base dn |
|
| 169 | + * |
|
| 170 | + * @return WizardResult |
|
| 171 | + * @throws \Exception |
|
| 172 | + */ |
|
| 173 | + public function countInBaseDN() { |
|
| 174 | + // we don't need to provide a filter in this case |
|
| 175 | + $total = $this->countEntries(null, 'objects'); |
|
| 176 | + if($total === false) { |
|
| 177 | + throw new \Exception('invalid results received'); |
|
| 178 | + } |
|
| 179 | + $this->result->addChange('ldap_test_base', $total); |
|
| 180 | + return $this->result; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * counts users with a specified attribute |
|
| 185 | + * @param string $attr |
|
| 186 | + * @param bool $existsCheck |
|
| 187 | + * @return int|bool |
|
| 188 | + */ |
|
| 189 | + public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
| 190 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 191 | + 'ldapPort', |
|
| 192 | + 'ldapBase', |
|
| 193 | + 'ldapUserFilter', |
|
| 194 | + ))) { |
|
| 195 | + return false; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 199 | + $this->configuration->ldapUserFilter, |
|
| 200 | + $attr . '=*' |
|
| 201 | + )); |
|
| 202 | + |
|
| 203 | + $limit = ($existsCheck === false) ? null : 1; |
|
| 204 | + |
|
| 205 | + return $this->access->countUsers($filter, array('dn'), $limit); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * detects the display name attribute. If a setting is already present that |
|
| 210 | + * returns at least one hit, the detection will be canceled. |
|
| 211 | + * @return WizardResult|bool |
|
| 212 | + * @throws \Exception |
|
| 213 | + */ |
|
| 214 | + public function detectUserDisplayNameAttribute() { |
|
| 215 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 216 | + 'ldapPort', |
|
| 217 | + 'ldapBase', |
|
| 218 | + 'ldapUserFilter', |
|
| 219 | + ))) { |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $attr = $this->configuration->ldapUserDisplayName; |
|
| 224 | + if ($attr !== '' && $attr !== 'displayName') { |
|
| 225 | + // most likely not the default value with upper case N, |
|
| 226 | + // verify it still produces a result |
|
| 227 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 228 | + if($count > 0) { |
|
| 229 | + //no change, but we sent it back to make sure the user interface |
|
| 230 | + //is still correct, even if the ajax call was cancelled meanwhile |
|
| 231 | + $this->result->addChange('ldap_display_name', $attr); |
|
| 232 | + return $this->result; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + // first attribute that has at least one result wins |
|
| 237 | + $displayNameAttrs = array('displayname', 'cn'); |
|
| 238 | + foreach ($displayNameAttrs as $attr) { |
|
| 239 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 240 | + |
|
| 241 | + if($count > 0) { |
|
| 242 | + $this->applyFind('ldap_display_name', $attr); |
|
| 243 | + return $this->result; |
|
| 244 | + } |
|
| 245 | + }; |
|
| 246 | + |
|
| 247 | + throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.')); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * detects the most often used email attribute for users applying to the |
|
| 252 | + * user list filter. If a setting is already present that returns at least |
|
| 253 | + * one hit, the detection will be canceled. |
|
| 254 | + * @return WizardResult|bool |
|
| 255 | + */ |
|
| 256 | + public function detectEmailAttribute() { |
|
| 257 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 258 | + 'ldapPort', |
|
| 259 | + 'ldapBase', |
|
| 260 | + 'ldapUserFilter', |
|
| 261 | + ))) { |
|
| 262 | + return false; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $attr = $this->configuration->ldapEmailAttribute; |
|
| 266 | + if ($attr !== '') { |
|
| 267 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 268 | + if($count > 0) { |
|
| 269 | + return false; |
|
| 270 | + } |
|
| 271 | + $writeLog = true; |
|
| 272 | + } else { |
|
| 273 | + $writeLog = false; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
| 277 | + $winner = ''; |
|
| 278 | + $maxUsers = 0; |
|
| 279 | + foreach($emailAttributes as $attr) { |
|
| 280 | + $count = $this->countUsersWithAttribute($attr); |
|
| 281 | + if($count > $maxUsers) { |
|
| 282 | + $maxUsers = $count; |
|
| 283 | + $winner = $attr; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + if($winner !== '') { |
|
| 288 | + $this->applyFind('ldap_email_attr', $winner); |
|
| 289 | + if($writeLog) { |
|
| 290 | + \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 291 | + 'automatically been reset, because the original value ' . |
|
| 292 | + 'did not return any results.', \OCP\Util::INFO); |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + return $this->result; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @return WizardResult |
|
| 301 | + * @throws \Exception |
|
| 302 | + */ |
|
| 303 | + public function determineAttributes() { |
|
| 304 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 305 | + 'ldapPort', |
|
| 306 | + 'ldapBase', |
|
| 307 | + 'ldapUserFilter', |
|
| 308 | + ))) { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $attributes = $this->getUserAttributes(); |
|
| 313 | + |
|
| 314 | + natcasesort($attributes); |
|
| 315 | + $attributes = array_values($attributes); |
|
| 316 | + |
|
| 317 | + $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
| 318 | + |
|
| 319 | + $selected = $this->configuration->ldapLoginFilterAttributes; |
|
| 320 | + if(is_array($selected) && !empty($selected)) { |
|
| 321 | + $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + return $this->result; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * detects the available LDAP attributes |
|
| 329 | + * @return array|false The instance's WizardResult instance |
|
| 330 | + * @throws \Exception |
|
| 331 | + */ |
|
| 332 | + private function getUserAttributes() { |
|
| 333 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 334 | + 'ldapPort', |
|
| 335 | + 'ldapBase', |
|
| 336 | + 'ldapUserFilter', |
|
| 337 | + ))) { |
|
| 338 | + return false; |
|
| 339 | + } |
|
| 340 | + $cr = $this->getConnection(); |
|
| 341 | + if(!$cr) { |
|
| 342 | + throw new \Exception('Could not connect to LDAP'); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + $base = $this->configuration->ldapBase[0]; |
|
| 346 | + $filter = $this->configuration->ldapUserFilter; |
|
| 347 | + $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
| 348 | + if(!$this->ldap->isResource($rr)) { |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 352 | + $attributes = $this->ldap->getAttributes($cr, $er); |
|
| 353 | + $pureAttributes = array(); |
|
| 354 | + for($i = 0; $i < $attributes['count']; $i++) { |
|
| 355 | + $pureAttributes[] = $attributes[$i]; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + return $pureAttributes; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * detects the available LDAP groups |
|
| 363 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 364 | + */ |
|
| 365 | + public function determineGroupsForGroups() { |
|
| 366 | + return $this->determineGroups('ldap_groupfilter_groups', |
|
| 367 | + 'ldapGroupFilterGroups', |
|
| 368 | + false); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * detects the available LDAP groups |
|
| 373 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 374 | + */ |
|
| 375 | + public function determineGroupsForUsers() { |
|
| 376 | + return $this->determineGroups('ldap_userfilter_groups', |
|
| 377 | + 'ldapUserFilterGroups'); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * detects the available LDAP groups |
|
| 382 | + * @param string $dbKey |
|
| 383 | + * @param string $confKey |
|
| 384 | + * @param bool $testMemberOf |
|
| 385 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 386 | + * @throws \Exception |
|
| 387 | + */ |
|
| 388 | + private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
| 389 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 390 | + 'ldapPort', |
|
| 391 | + 'ldapBase', |
|
| 392 | + ))) { |
|
| 393 | + return false; |
|
| 394 | + } |
|
| 395 | + $cr = $this->getConnection(); |
|
| 396 | + if(!$cr) { |
|
| 397 | + throw new \Exception('Could not connect to LDAP'); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $this->fetchGroups($dbKey, $confKey); |
|
| 401 | + |
|
| 402 | + if($testMemberOf) { |
|
| 403 | + $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
| 404 | + $this->result->markChange(); |
|
| 405 | + if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 406 | + throw new \Exception('memberOf is not supported by the server'); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + return $this->result; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * fetches all groups from LDAP and adds them to the result object |
|
| 415 | + * |
|
| 416 | + * @param string $dbKey |
|
| 417 | + * @param string $confKey |
|
| 418 | + * @return array $groupEntries |
|
| 419 | + * @throws \Exception |
|
| 420 | + */ |
|
| 421 | + public function fetchGroups($dbKey, $confKey) { |
|
| 422 | + $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
|
| 423 | + |
|
| 424 | + $filterParts = array(); |
|
| 425 | + foreach($obclasses as $obclass) { |
|
| 426 | + $filterParts[] = 'objectclass='.$obclass; |
|
| 427 | + } |
|
| 428 | + //we filter for everything |
|
| 429 | + //- that looks like a group and |
|
| 430 | + //- has the group display name set |
|
| 431 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 432 | + $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
| 433 | + |
|
| 434 | + $groupNames = array(); |
|
| 435 | + $groupEntries = array(); |
|
| 436 | + $limit = 400; |
|
| 437 | + $offset = 0; |
|
| 438 | + do { |
|
| 439 | + // we need to request dn additionally here, otherwise memberOf |
|
| 440 | + // detection will fail later |
|
| 441 | + $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
| 442 | + foreach($result as $item) { |
|
| 443 | + if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 444 | + // just in case - no issue known |
|
| 445 | + continue; |
|
| 446 | + } |
|
| 447 | + $groupNames[] = $item['cn'][0]; |
|
| 448 | + $groupEntries[] = $item; |
|
| 449 | + } |
|
| 450 | + $offset += $limit; |
|
| 451 | + } while ($this->access->hasMoreResults()); |
|
| 452 | + |
|
| 453 | + if(count($groupNames) > 0) { |
|
| 454 | + natsort($groupNames); |
|
| 455 | + $this->result->addOptions($dbKey, array_values($groupNames)); |
|
| 456 | + } else { |
|
| 457 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $setFeatures = $this->configuration->$confKey; |
|
| 461 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 462 | + //something is already configured? pre-select it. |
|
| 463 | + $this->result->addChange($dbKey, $setFeatures); |
|
| 464 | + } |
|
| 465 | + return $groupEntries; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + public function determineGroupMemberAssoc() { |
|
| 469 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 470 | + 'ldapPort', |
|
| 471 | + 'ldapGroupFilter', |
|
| 472 | + ))) { |
|
| 473 | + return false; |
|
| 474 | + } |
|
| 475 | + $attribute = $this->detectGroupMemberAssoc(); |
|
| 476 | + if($attribute === false) { |
|
| 477 | + return false; |
|
| 478 | + } |
|
| 479 | + $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
| 480 | + $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
| 481 | + |
|
| 482 | + return $this->result; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Detects the available object classes |
|
| 487 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 488 | + * @throws \Exception |
|
| 489 | + */ |
|
| 490 | + public function determineGroupObjectClasses() { |
|
| 491 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 492 | + 'ldapPort', |
|
| 493 | + 'ldapBase', |
|
| 494 | + ))) { |
|
| 495 | + return false; |
|
| 496 | + } |
|
| 497 | + $cr = $this->getConnection(); |
|
| 498 | + if(!$cr) { |
|
| 499 | + throw new \Exception('Could not connect to LDAP'); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
| 503 | + $this->determineFeature($obclasses, |
|
| 504 | + 'objectclass', |
|
| 505 | + 'ldap_groupfilter_objectclass', |
|
| 506 | + 'ldapGroupFilterObjectclass', |
|
| 507 | + false); |
|
| 508 | + |
|
| 509 | + return $this->result; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * detects the available object classes |
|
| 514 | + * @return WizardResult |
|
| 515 | + * @throws \Exception |
|
| 516 | + */ |
|
| 517 | + public function determineUserObjectClasses() { |
|
| 518 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 519 | + 'ldapPort', |
|
| 520 | + 'ldapBase', |
|
| 521 | + ))) { |
|
| 522 | + return false; |
|
| 523 | + } |
|
| 524 | + $cr = $this->getConnection(); |
|
| 525 | + if(!$cr) { |
|
| 526 | + throw new \Exception('Could not connect to LDAP'); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
| 530 | + 'user', 'posixAccount', '*'); |
|
| 531 | + $filter = $this->configuration->ldapUserFilter; |
|
| 532 | + //if filter is empty, it is probably the first time the wizard is called |
|
| 533 | + //then, apply suggestions. |
|
| 534 | + $this->determineFeature($obclasses, |
|
| 535 | + 'objectclass', |
|
| 536 | + 'ldap_userfilter_objectclass', |
|
| 537 | + 'ldapUserFilterObjectclass', |
|
| 538 | + empty($filter)); |
|
| 539 | + |
|
| 540 | + return $this->result; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * @return WizardResult|false |
|
| 545 | + * @throws \Exception |
|
| 546 | + */ |
|
| 547 | + public function getGroupFilter() { |
|
| 548 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 549 | + 'ldapPort', |
|
| 550 | + 'ldapBase', |
|
| 551 | + ))) { |
|
| 552 | + return false; |
|
| 553 | + } |
|
| 554 | + //make sure the use display name is set |
|
| 555 | + $displayName = $this->configuration->ldapGroupDisplayName; |
|
| 556 | + if ($displayName === '') { |
|
| 557 | + $d = $this->configuration->getDefaults(); |
|
| 558 | + $this->applyFind('ldap_group_display_name', |
|
| 559 | + $d['ldap_group_display_name']); |
|
| 560 | + } |
|
| 561 | + $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
| 562 | + |
|
| 563 | + $this->applyFind('ldap_group_filter', $filter); |
|
| 564 | + return $this->result; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * @return WizardResult|false |
|
| 569 | + * @throws \Exception |
|
| 570 | + */ |
|
| 571 | + public function getUserListFilter() { |
|
| 572 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 573 | + 'ldapPort', |
|
| 574 | + 'ldapBase', |
|
| 575 | + ))) { |
|
| 576 | + return false; |
|
| 577 | + } |
|
| 578 | + //make sure the use display name is set |
|
| 579 | + $displayName = $this->configuration->ldapUserDisplayName; |
|
| 580 | + if ($displayName === '') { |
|
| 581 | + $d = $this->configuration->getDefaults(); |
|
| 582 | + $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
| 583 | + } |
|
| 584 | + $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
| 585 | + if(!$filter) { |
|
| 586 | + throw new \Exception('Cannot create filter'); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $this->applyFind('ldap_userlist_filter', $filter); |
|
| 590 | + return $this->result; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * @return bool|WizardResult |
|
| 595 | + * @throws \Exception |
|
| 596 | + */ |
|
| 597 | + public function getUserLoginFilter() { |
|
| 598 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 599 | + 'ldapPort', |
|
| 600 | + 'ldapBase', |
|
| 601 | + 'ldapUserFilter', |
|
| 602 | + ))) { |
|
| 603 | + return false; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
| 607 | + if(!$filter) { |
|
| 608 | + throw new \Exception('Cannot create filter'); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + $this->applyFind('ldap_login_filter', $filter); |
|
| 612 | + return $this->result; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * @return bool|WizardResult |
|
| 617 | + * @param string $loginName |
|
| 618 | + * @throws \Exception |
|
| 619 | + */ |
|
| 620 | + public function testLoginName($loginName) { |
|
| 621 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 622 | + 'ldapPort', |
|
| 623 | + 'ldapBase', |
|
| 624 | + 'ldapLoginFilter', |
|
| 625 | + ))) { |
|
| 626 | + return false; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + $cr = $this->access->connection->getConnectionResource(); |
|
| 630 | + if(!$this->ldap->isResource($cr)) { |
|
| 631 | + throw new \Exception('connection error'); |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 635 | + === false) { |
|
| 636 | + throw new \Exception('missing placeholder'); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + $users = $this->access->countUsersByLoginName($loginName); |
|
| 640 | + if($this->ldap->errno($cr) !== 0) { |
|
| 641 | + throw new \Exception($this->ldap->error($cr)); |
|
| 642 | + } |
|
| 643 | + $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
| 644 | + $this->result->addChange('ldap_test_loginname', $users); |
|
| 645 | + $this->result->addChange('ldap_test_effective_filter', $filter); |
|
| 646 | + return $this->result; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Tries to determine the port, requires given Host, User DN and Password |
|
| 651 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
| 652 | + * @throws \Exception |
|
| 653 | + */ |
|
| 654 | + public function guessPortAndTLS() { |
|
| 655 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 656 | + ))) { |
|
| 657 | + return false; |
|
| 658 | + } |
|
| 659 | + $this->checkHost(); |
|
| 660 | + $portSettings = $this->getPortSettingsToTry(); |
|
| 661 | + |
|
| 662 | + if(!is_array($portSettings)) { |
|
| 663 | + throw new \Exception(print_r($portSettings, true)); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + //proceed from the best configuration and return on first success |
|
| 667 | + foreach($portSettings as $setting) { |
|
| 668 | + $p = $setting['port']; |
|
| 669 | + $t = $setting['tls']; |
|
| 670 | + \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 671 | + //connectAndBind may throw Exception, it needs to be catched by the |
|
| 672 | + //callee of this method |
|
| 673 | + |
|
| 674 | + try { |
|
| 675 | + $settingsFound = $this->connectAndBind($p, $t); |
|
| 676 | + } catch (\Exception $e) { |
|
| 677 | + // any reply other than -1 (= cannot connect) is already okay, |
|
| 678 | + // because then we found the server |
|
| 679 | + // unavailable startTLS returns -11 |
|
| 680 | + if($e->getCode() > 0) { |
|
| 681 | + $settingsFound = true; |
|
| 682 | + } else { |
|
| 683 | + throw $e; |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + if ($settingsFound === true) { |
|
| 688 | + $config = array( |
|
| 689 | + 'ldapPort' => $p, |
|
| 690 | + 'ldapTLS' => intval($t) |
|
| 691 | + ); |
|
| 692 | + $this->configuration->setConfiguration($config); |
|
| 693 | + \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 694 | + $this->result->addChange('ldap_port', $p); |
|
| 695 | + return $this->result; |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + //custom port, undetected (we do not brute force) |
|
| 700 | + return false; |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * tries to determine a base dn from User DN or LDAP Host |
|
| 705 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
| 706 | + */ |
|
| 707 | + public function guessBaseDN() { |
|
| 708 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 709 | + 'ldapPort', |
|
| 710 | + ))) { |
|
| 711 | + return false; |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + //check whether a DN is given in the agent name (99.9% of all cases) |
|
| 715 | + $base = null; |
|
| 716 | + $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
| 717 | + if($i !== false) { |
|
| 718 | + $base = substr($this->configuration->ldapAgentName, $i); |
|
| 719 | + if($this->testBaseDN($base)) { |
|
| 720 | + $this->applyFind('ldap_base', $base); |
|
| 721 | + return $this->result; |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + //this did not help :( |
|
| 726 | + //Let's see whether we can parse the Host URL and convert the domain to |
|
| 727 | + //a base DN |
|
| 728 | + $helper = new Helper(\OC::$server->getConfig()); |
|
| 729 | + $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
| 730 | + if(!$domain) { |
|
| 731 | + return false; |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + $dparts = explode('.', $domain); |
|
| 735 | + while(count($dparts) > 0) { |
|
| 736 | + $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 737 | + if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
| 738 | + $this->applyFind('ldap_base', $base2); |
|
| 739 | + return $this->result; |
|
| 740 | + } |
|
| 741 | + array_shift($dparts); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + return false; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * sets the found value for the configuration key in the WizardResult |
|
| 749 | + * as well as in the Configuration instance |
|
| 750 | + * @param string $key the configuration key |
|
| 751 | + * @param string $value the (detected) value |
|
| 752 | + * |
|
| 753 | + */ |
|
| 754 | + private function applyFind($key, $value) { |
|
| 755 | + $this->result->addChange($key, $value); |
|
| 756 | + $this->configuration->setConfiguration(array($key => $value)); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * Checks, whether a port was entered in the Host configuration |
|
| 761 | + * field. In this case the port will be stripped off, but also stored as |
|
| 762 | + * setting. |
|
| 763 | + */ |
|
| 764 | + private function checkHost() { |
|
| 765 | + $host = $this->configuration->ldapHost; |
|
| 766 | + $hostInfo = parse_url($host); |
|
| 767 | + |
|
| 768 | + //removes Port from Host |
|
| 769 | + if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 770 | + $port = $hostInfo['port']; |
|
| 771 | + $host = str_replace(':'.$port, '', $host); |
|
| 772 | + $this->applyFind('ldap_host', $host); |
|
| 773 | + $this->applyFind('ldap_port', $port); |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * tries to detect the group member association attribute which is |
|
| 779 | + * one of 'uniqueMember', 'memberUid', 'member', 'gidNumber' |
|
| 780 | + * @return string|false, string with the attribute name, false on error |
|
| 781 | + * @throws \Exception |
|
| 782 | + */ |
|
| 783 | + private function detectGroupMemberAssoc() { |
|
| 784 | + $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
|
| 785 | + $filter = $this->configuration->ldapGroupFilter; |
|
| 786 | + if(empty($filter)) { |
|
| 787 | + return false; |
|
| 788 | + } |
|
| 789 | + $cr = $this->getConnection(); |
|
| 790 | + if(!$cr) { |
|
| 791 | + throw new \Exception('Could not connect to LDAP'); |
|
| 792 | + } |
|
| 793 | + $base = $this->configuration->ldapBase[0]; |
|
| 794 | + $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
| 795 | + if(!$this->ldap->isResource($rr)) { |
|
| 796 | + return false; |
|
| 797 | + } |
|
| 798 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 799 | + while(is_resource($er)) { |
|
| 800 | + $this->ldap->getDN($cr, $er); |
|
| 801 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 802 | + $result = array(); |
|
| 803 | + $possibleAttrsCount = count($possibleAttrs); |
|
| 804 | + for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 805 | + if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 806 | + $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + if(!empty($result)) { |
|
| 810 | + natsort($result); |
|
| 811 | + return key($result); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + $er = $this->ldap->nextEntry($cr, $er); |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + return false; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Checks whether for a given BaseDN results will be returned |
|
| 822 | + * @param string $base the BaseDN to test |
|
| 823 | + * @return bool true on success, false otherwise |
|
| 824 | + * @throws \Exception |
|
| 825 | + */ |
|
| 826 | + private function testBaseDN($base) { |
|
| 827 | + $cr = $this->getConnection(); |
|
| 828 | + if(!$cr) { |
|
| 829 | + throw new \Exception('Could not connect to LDAP'); |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + //base is there, let's validate it. If we search for anything, we should |
|
| 833 | + //get a result set > 0 on a proper base |
|
| 834 | + $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
| 835 | + if(!$this->ldap->isResource($rr)) { |
|
| 836 | + $errorNo = $this->ldap->errno($cr); |
|
| 837 | + $errorMsg = $this->ldap->error($cr); |
|
| 838 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
| 839 | + ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
| 840 | + return false; |
|
| 841 | + } |
|
| 842 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
| 843 | + return ($entries !== false) && ($entries > 0); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Checks whether the server supports memberOf in LDAP Filter. |
|
| 848 | + * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
| 849 | + * a configured objectClass. I.e. not necessarily for all available groups |
|
| 850 | + * memberOf does work. |
|
| 851 | + * |
|
| 852 | + * @return bool true if it does, false otherwise |
|
| 853 | + * @throws \Exception |
|
| 854 | + */ |
|
| 855 | + private function testMemberOf() { |
|
| 856 | + $cr = $this->getConnection(); |
|
| 857 | + if(!$cr) { |
|
| 858 | + throw new \Exception('Could not connect to LDAP'); |
|
| 859 | + } |
|
| 860 | + $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
| 861 | + if(is_int($result) && $result > 0) { |
|
| 862 | + return true; |
|
| 863 | + } |
|
| 864 | + return false; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + /** |
|
| 868 | + * creates an LDAP Filter from given configuration |
|
| 869 | + * @param integer $filterType int, for which use case the filter shall be created |
|
| 870 | + * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
| 871 | + * self::LFILTER_GROUP_LIST |
|
| 872 | + * @return string|false string with the filter on success, false otherwise |
|
| 873 | + * @throws \Exception |
|
| 874 | + */ |
|
| 875 | + private function composeLdapFilter($filterType) { |
|
| 876 | + $filter = ''; |
|
| 877 | + $parts = 0; |
|
| 878 | + switch ($filterType) { |
|
| 879 | + case self::LFILTER_USER_LIST: |
|
| 880 | + $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
| 881 | + //glue objectclasses |
|
| 882 | + if(is_array($objcs) && count($objcs) > 0) { |
|
| 883 | + $filter .= '(|'; |
|
| 884 | + foreach($objcs as $objc) { |
|
| 885 | + $filter .= '(objectclass=' . $objc . ')'; |
|
| 886 | + } |
|
| 887 | + $filter .= ')'; |
|
| 888 | + $parts++; |
|
| 889 | + } |
|
| 890 | + //glue group memberships |
|
| 891 | + if($this->configuration->hasMemberOfFilterSupport) { |
|
| 892 | + $cns = $this->configuration->ldapUserFilterGroups; |
|
| 893 | + if(is_array($cns) && count($cns) > 0) { |
|
| 894 | + $filter .= '(|'; |
|
| 895 | + $cr = $this->getConnection(); |
|
| 896 | + if(!$cr) { |
|
| 897 | + throw new \Exception('Could not connect to LDAP'); |
|
| 898 | + } |
|
| 899 | + $base = $this->configuration->ldapBase[0]; |
|
| 900 | + foreach($cns as $cn) { |
|
| 901 | + $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 902 | + if(!$this->ldap->isResource($rr)) { |
|
| 903 | + continue; |
|
| 904 | + } |
|
| 905 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 906 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 907 | + $dn = $this->ldap->getDN($cr, $er); |
|
| 908 | + if ($dn == false || $dn === '') { |
|
| 909 | + continue; |
|
| 910 | + } |
|
| 911 | + $filterPart = '(memberof=' . $dn . ')'; |
|
| 912 | + if(isset($attrs['primaryGroupToken'])) { |
|
| 913 | + $pgt = $attrs['primaryGroupToken'][0]; |
|
| 914 | + $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 915 | + $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 916 | + } |
|
| 917 | + $filter .= $filterPart; |
|
| 918 | + } |
|
| 919 | + $filter .= ')'; |
|
| 920 | + } |
|
| 921 | + $parts++; |
|
| 922 | + } |
|
| 923 | + //wrap parts in AND condition |
|
| 924 | + if($parts > 1) { |
|
| 925 | + $filter = '(&' . $filter . ')'; |
|
| 926 | + } |
|
| 927 | + if ($filter === '') { |
|
| 928 | + $filter = '(objectclass=*)'; |
|
| 929 | + } |
|
| 930 | + break; |
|
| 931 | + |
|
| 932 | + case self::LFILTER_GROUP_LIST: |
|
| 933 | + $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
| 934 | + //glue objectclasses |
|
| 935 | + if(is_array($objcs) && count($objcs) > 0) { |
|
| 936 | + $filter .= '(|'; |
|
| 937 | + foreach($objcs as $objc) { |
|
| 938 | + $filter .= '(objectclass=' . $objc . ')'; |
|
| 939 | + } |
|
| 940 | + $filter .= ')'; |
|
| 941 | + $parts++; |
|
| 942 | + } |
|
| 943 | + //glue group memberships |
|
| 944 | + $cns = $this->configuration->ldapGroupFilterGroups; |
|
| 945 | + if(is_array($cns) && count($cns) > 0) { |
|
| 946 | + $filter .= '(|'; |
|
| 947 | + foreach($cns as $cn) { |
|
| 948 | + $filter .= '(cn=' . $cn . ')'; |
|
| 949 | + } |
|
| 950 | + $filter .= ')'; |
|
| 951 | + } |
|
| 952 | + $parts++; |
|
| 953 | + //wrap parts in AND condition |
|
| 954 | + if($parts > 1) { |
|
| 955 | + $filter = '(&' . $filter . ')'; |
|
| 956 | + } |
|
| 957 | + break; |
|
| 958 | + |
|
| 959 | + case self::LFILTER_LOGIN: |
|
| 960 | + $ulf = $this->configuration->ldapUserFilter; |
|
| 961 | + $loginpart = '=%uid'; |
|
| 962 | + $filterUsername = ''; |
|
| 963 | + $userAttributes = $this->getUserAttributes(); |
|
| 964 | + $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
| 965 | + $parts = 0; |
|
| 966 | + |
|
| 967 | + if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 968 | + $attr = ''; |
|
| 969 | + if(isset($userAttributes['uid'])) { |
|
| 970 | + $attr = 'uid'; |
|
| 971 | + } else if(isset($userAttributes['samaccountname'])) { |
|
| 972 | + $attr = 'samaccountname'; |
|
| 973 | + } else if(isset($userAttributes['cn'])) { |
|
| 974 | + //fallback |
|
| 975 | + $attr = 'cn'; |
|
| 976 | + } |
|
| 977 | + if ($attr !== '') { |
|
| 978 | + $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 979 | + $parts++; |
|
| 980 | + } |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + $filterEmail = ''; |
|
| 984 | + if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 985 | + $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
| 986 | + $parts++; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + $filterAttributes = ''; |
|
| 990 | + $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
| 991 | + if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 992 | + $filterAttributes = '(|'; |
|
| 993 | + foreach($attrsToFilter as $attribute) { |
|
| 994 | + $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 995 | + } |
|
| 996 | + $filterAttributes .= ')'; |
|
| 997 | + $parts++; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + $filterLogin = ''; |
|
| 1001 | + if($parts > 1) { |
|
| 1002 | + $filterLogin = '(|'; |
|
| 1003 | + } |
|
| 1004 | + $filterLogin .= $filterUsername; |
|
| 1005 | + $filterLogin .= $filterEmail; |
|
| 1006 | + $filterLogin .= $filterAttributes; |
|
| 1007 | + if($parts > 1) { |
|
| 1008 | + $filterLogin .= ')'; |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + $filter = '(&'.$ulf.$filterLogin.')'; |
|
| 1012 | + break; |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
| 1016 | + |
|
| 1017 | + return $filter; |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + /** |
|
| 1021 | + * Connects and Binds to an LDAP Server |
|
| 1022 | + * @param int $port the port to connect with |
|
| 1023 | + * @param bool $tls whether startTLS is to be used |
|
| 1024 | + * @param bool $ncc no certificate check |
|
| 1025 | + * @return bool |
|
| 1026 | + * @throws \Exception |
|
| 1027 | + */ |
|
| 1028 | + private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
|
| 1029 | + if($ncc) { |
|
| 1030 | + $originalLDAPTLS_REQCERT = strval(getenv('LDAPTLS_REQCERT')); |
|
| 1031 | + //No certificate check |
|
| 1032 | + putenv('LDAPTLS_REQCERT=never'); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + //connect, does not really trigger any server communication |
|
| 1036 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
|
| 1037 | + $host = $this->configuration->ldapHost; |
|
| 1038 | + $hostInfo = parse_url($host); |
|
| 1039 | + if(!$hostInfo) { |
|
| 1040 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1041 | + } |
|
| 1042 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
| 1043 | + $cr = $this->ldap->connect($host, $port); |
|
| 1044 | + if(!is_resource($cr)) { |
|
| 1045 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1046 | + } |
|
| 1047 | + |
|
| 1048 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Setting LDAP Options ', \OCP\Util::DEBUG); |
|
| 1049 | + //set LDAP options |
|
| 1050 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1051 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1052 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1053 | + |
|
| 1054 | + try { |
|
| 1055 | + if($tls) { |
|
| 1056 | + $isTlsWorking = @$this->ldap->startTls($cr); |
|
| 1057 | + if(!$isTlsWorking) { |
|
| 1058 | + return false; |
|
| 1059 | + } |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
| 1063 | + //interesting part: do the bind! |
|
| 1064 | + $login = $this->ldap->bind($cr, |
|
| 1065 | + $this->configuration->ldapAgentName, |
|
| 1066 | + $this->configuration->ldapAgentPassword |
|
| 1067 | + ); |
|
| 1068 | + $errNo = $this->ldap->errno($cr); |
|
| 1069 | + $error = ldap_error($cr); |
|
| 1070 | + $this->ldap->unbind($cr); |
|
| 1071 | + } catch(ServerNotAvailableException $e) { |
|
| 1072 | + if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1073 | + putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1074 | + } |
|
| 1075 | + return false; |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1079 | + putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1080 | + } |
|
| 1081 | + |
|
| 1082 | + if($login === true) { |
|
| 1083 | + $this->ldap->unbind($cr); |
|
| 1084 | + if($ncc) { |
|
| 1085 | + throw new \Exception('Certificate cannot be validated.'); |
|
| 1086 | + } |
|
| 1087 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
|
| 1088 | + return true; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + if($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
| 1092 | + //host, port or TLS wrong |
|
| 1093 | + return false; |
|
| 1094 | + } else if ($errNo === 2) { |
|
| 1095 | + return $this->connectAndBind($port, $tls, true); |
|
| 1096 | + } |
|
| 1097 | + throw new \Exception($error, $errNo); |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + /** |
|
| 1101 | + * checks whether a valid combination of agent and password has been |
|
| 1102 | + * provided (either two values or nothing for anonymous connect) |
|
| 1103 | + * @return bool, true if everything is fine, false otherwise |
|
| 1104 | + */ |
|
| 1105 | + private function checkAgentRequirements() { |
|
| 1106 | + $agent = $this->configuration->ldapAgentName; |
|
| 1107 | + $pwd = $this->configuration->ldapAgentPassword; |
|
| 1108 | + |
|
| 1109 | + return |
|
| 1110 | + ($agent !== '' && $pwd !== '') |
|
| 1111 | + || ($agent === '' && $pwd === '') |
|
| 1112 | + ; |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * @param array $reqs |
|
| 1117 | + * @return bool |
|
| 1118 | + */ |
|
| 1119 | + private function checkRequirements($reqs) { |
|
| 1120 | + $this->checkAgentRequirements(); |
|
| 1121 | + foreach($reqs as $option) { |
|
| 1122 | + $value = $this->configuration->$option; |
|
| 1123 | + if(empty($value)) { |
|
| 1124 | + return false; |
|
| 1125 | + } |
|
| 1126 | + } |
|
| 1127 | + return true; |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + /** |
|
| 1131 | + * does a cumulativeSearch on LDAP to get different values of a |
|
| 1132 | + * specified attribute |
|
| 1133 | + * @param string[] $filters array, the filters that shall be used in the search |
|
| 1134 | + * @param string $attr the attribute of which a list of values shall be returned |
|
| 1135 | + * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
| 1136 | + * The lower, the faster |
|
| 1137 | + * @param string $maxF string. if not null, this variable will have the filter that |
|
| 1138 | + * yields most result entries |
|
| 1139 | + * @return array|false an array with the values on success, false otherwise |
|
| 1140 | + */ |
|
| 1141 | + public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
| 1142 | + $dnRead = array(); |
|
| 1143 | + $foundItems = array(); |
|
| 1144 | + $maxEntries = 0; |
|
| 1145 | + if(!is_array($this->configuration->ldapBase) |
|
| 1146 | + || !isset($this->configuration->ldapBase[0])) { |
|
| 1147 | + return false; |
|
| 1148 | + } |
|
| 1149 | + $base = $this->configuration->ldapBase[0]; |
|
| 1150 | + $cr = $this->getConnection(); |
|
| 1151 | + if(!$this->ldap->isResource($cr)) { |
|
| 1152 | + return false; |
|
| 1153 | + } |
|
| 1154 | + $lastFilter = null; |
|
| 1155 | + if(isset($filters[count($filters)-1])) { |
|
| 1156 | + $lastFilter = $filters[count($filters)-1]; |
|
| 1157 | + } |
|
| 1158 | + foreach($filters as $filter) { |
|
| 1159 | + if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1160 | + //skip when the filter is a wildcard and results were found |
|
| 1161 | + continue; |
|
| 1162 | + } |
|
| 1163 | + // 20k limit for performance and reason |
|
| 1164 | + $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
| 1165 | + if(!$this->ldap->isResource($rr)) { |
|
| 1166 | + continue; |
|
| 1167 | + } |
|
| 1168 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
| 1169 | + $getEntryFunc = 'firstEntry'; |
|
| 1170 | + if(($entries !== false) && ($entries > 0)) { |
|
| 1171 | + if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1172 | + $maxEntries = $entries; |
|
| 1173 | + $maxF = $filter; |
|
| 1174 | + } |
|
| 1175 | + $dnReadCount = 0; |
|
| 1176 | + do { |
|
| 1177 | + $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
| 1178 | + $getEntryFunc = 'nextEntry'; |
|
| 1179 | + if(!$this->ldap->isResource($entry)) { |
|
| 1180 | + continue 2; |
|
| 1181 | + } |
|
| 1182 | + $rr = $entry; //will be expected by nextEntry next round |
|
| 1183 | + $attributes = $this->ldap->getAttributes($cr, $entry); |
|
| 1184 | + $dn = $this->ldap->getDN($cr, $entry); |
|
| 1185 | + if($dn === false || in_array($dn, $dnRead)) { |
|
| 1186 | + continue; |
|
| 1187 | + } |
|
| 1188 | + $newItems = array(); |
|
| 1189 | + $state = $this->getAttributeValuesFromEntry($attributes, |
|
| 1190 | + $attr, |
|
| 1191 | + $newItems); |
|
| 1192 | + $dnReadCount++; |
|
| 1193 | + $foundItems = array_merge($foundItems, $newItems); |
|
| 1194 | + $this->resultCache[$dn][$attr] = $newItems; |
|
| 1195 | + $dnRead[] = $dn; |
|
| 1196 | + } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1197 | + || $this->ldap->isResource($entry)) |
|
| 1198 | + && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
| 1199 | + } |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + return array_unique($foundItems); |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * determines if and which $attr are available on the LDAP server |
|
| 1207 | + * @param string[] $objectclasses the objectclasses to use as search filter |
|
| 1208 | + * @param string $attr the attribute to look for |
|
| 1209 | + * @param string $dbkey the dbkey of the setting the feature is connected to |
|
| 1210 | + * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
| 1211 | + * Configuration class |
|
| 1212 | + * @param bool $po whether the objectClass with most result entries |
|
| 1213 | + * shall be pre-selected via the result |
|
| 1214 | + * @return array|false list of found items. |
|
| 1215 | + * @throws \Exception |
|
| 1216 | + */ |
|
| 1217 | + private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
| 1218 | + $cr = $this->getConnection(); |
|
| 1219 | + if(!$cr) { |
|
| 1220 | + throw new \Exception('Could not connect to LDAP'); |
|
| 1221 | + } |
|
| 1222 | + $p = 'objectclass='; |
|
| 1223 | + foreach($objectclasses as $key => $value) { |
|
| 1224 | + $objectclasses[$key] = $p.$value; |
|
| 1225 | + } |
|
| 1226 | + $maxEntryObjC = ''; |
|
| 1227 | + |
|
| 1228 | + //how deep to dig? |
|
| 1229 | + //When looking for objectclasses, testing few entries is sufficient, |
|
| 1230 | + $dig = 3; |
|
| 1231 | + |
|
| 1232 | + $availableFeatures = |
|
| 1233 | + $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
| 1234 | + $dig, $maxEntryObjC); |
|
| 1235 | + if(is_array($availableFeatures) |
|
| 1236 | + && count($availableFeatures) > 0) { |
|
| 1237 | + natcasesort($availableFeatures); |
|
| 1238 | + //natcasesort keeps indices, but we must get rid of them for proper |
|
| 1239 | + //sorting in the web UI. Therefore: array_values |
|
| 1240 | + $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
| 1241 | + } else { |
|
| 1242 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + $setFeatures = $this->configuration->$confkey; |
|
| 1246 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1247 | + //something is already configured? pre-select it. |
|
| 1248 | + $this->result->addChange($dbkey, $setFeatures); |
|
| 1249 | + } else if ($po && $maxEntryObjC !== '') { |
|
| 1250 | + //pre-select objectclass with most result entries |
|
| 1251 | + $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
| 1252 | + $this->applyFind($dbkey, $maxEntryObjC); |
|
| 1253 | + $this->result->addChange($dbkey, $maxEntryObjC); |
|
| 1254 | + } |
|
| 1255 | + |
|
| 1256 | + return $availableFeatures; |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + /** |
|
| 1260 | + * appends a list of values fr |
|
| 1261 | + * @param resource $result the return value from ldap_get_attributes |
|
| 1262 | + * @param string $attribute the attribute values to look for |
|
| 1263 | + * @param array &$known new values will be appended here |
|
| 1264 | + * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
| 1265 | + * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
| 1266 | + */ |
|
| 1267 | + private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
| 1268 | + if(!is_array($result) |
|
| 1269 | + || !isset($result['count']) |
|
| 1270 | + || !$result['count'] > 0) { |
|
| 1271 | + return self::LRESULT_PROCESSED_INVALID; |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + // strtolower on all keys for proper comparison |
|
| 1275 | + $result = \OCP\Util::mb_array_change_key_case($result); |
|
| 1276 | + $attribute = strtolower($attribute); |
|
| 1277 | + if(isset($result[$attribute])) { |
|
| 1278 | + foreach($result[$attribute] as $key => $val) { |
|
| 1279 | + if($key === 'count') { |
|
| 1280 | + continue; |
|
| 1281 | + } |
|
| 1282 | + if(!in_array($val, $known)) { |
|
| 1283 | + $known[] = $val; |
|
| 1284 | + } |
|
| 1285 | + } |
|
| 1286 | + return self::LRESULT_PROCESSED_OK; |
|
| 1287 | + } else { |
|
| 1288 | + return self::LRESULT_PROCESSED_SKIP; |
|
| 1289 | + } |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * @return bool|mixed |
|
| 1294 | + */ |
|
| 1295 | + private function getConnection() { |
|
| 1296 | + if(!is_null($this->cr)) { |
|
| 1297 | + return $this->cr; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + $cr = $this->ldap->connect( |
|
| 1301 | + $this->configuration->ldapHost, |
|
| 1302 | + $this->configuration->ldapPort |
|
| 1303 | + ); |
|
| 1304 | + |
|
| 1305 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1306 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1307 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1308 | + if($this->configuration->ldapTLS === 1) { |
|
| 1309 | + $this->ldap->startTls($cr); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + $lo = @$this->ldap->bind($cr, |
|
| 1313 | + $this->configuration->ldapAgentName, |
|
| 1314 | + $this->configuration->ldapAgentPassword); |
|
| 1315 | + if($lo === true) { |
|
| 1316 | + $this->$cr = $cr; |
|
| 1317 | + return $cr; |
|
| 1318 | + } |
|
| 1319 | + |
|
| 1320 | + return false; |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + /** |
|
| 1324 | + * @return array |
|
| 1325 | + */ |
|
| 1326 | + private function getDefaultLdapPortSettings() { |
|
| 1327 | + static $settings = array( |
|
| 1328 | + array('port' => 7636, 'tls' => false), |
|
| 1329 | + array('port' => 636, 'tls' => false), |
|
| 1330 | + array('port' => 7389, 'tls' => true), |
|
| 1331 | + array('port' => 389, 'tls' => true), |
|
| 1332 | + array('port' => 7389, 'tls' => false), |
|
| 1333 | + array('port' => 389, 'tls' => false), |
|
| 1334 | + ); |
|
| 1335 | + return $settings; |
|
| 1336 | + } |
|
| 1337 | + |
|
| 1338 | + /** |
|
| 1339 | + * @return array |
|
| 1340 | + */ |
|
| 1341 | + private function getPortSettingsToTry() { |
|
| 1342 | + //389 ← LDAP / Unencrypted or StartTLS |
|
| 1343 | + //636 ← LDAPS / SSL |
|
| 1344 | + //7xxx ← UCS. need to be checked first, because both ports may be open |
|
| 1345 | + $host = $this->configuration->ldapHost; |
|
| 1346 | + $port = intval($this->configuration->ldapPort); |
|
| 1347 | + $portSettings = array(); |
|
| 1348 | + |
|
| 1349 | + //In case the port is already provided, we will check this first |
|
| 1350 | + if($port > 0) { |
|
| 1351 | + $hostInfo = parse_url($host); |
|
| 1352 | + if(!(is_array($hostInfo) |
|
| 1353 | + && isset($hostInfo['scheme']) |
|
| 1354 | + && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
| 1355 | + $portSettings[] = array('port' => $port, 'tls' => true); |
|
| 1356 | + } |
|
| 1357 | + $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1358 | + } |
|
| 1359 | + |
|
| 1360 | + //default ports |
|
| 1361 | + $portSettings = array_merge($portSettings, |
|
| 1362 | + $this->getDefaultLdapPortSettings()); |
|
| 1363 | + |
|
| 1364 | + return $portSettings; |
|
| 1365 | + } |
|
| 1366 | 1366 | |
| 1367 | 1367 | |
| 1368 | 1368 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
| 69 | 69 | parent::__construct($ldap); |
| 70 | 70 | $this->configuration = $configuration; |
| 71 | - if(is_null(Wizard::$l)) { |
|
| 71 | + if (is_null(Wizard::$l)) { |
|
| 72 | 72 | Wizard::$l = \OC::$server->getL10N('user_ldap'); |
| 73 | 73 | } |
| 74 | 74 | $this->access = $access; |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | public function __destruct() { |
| 79 | - if($this->result->hasChanges()) { |
|
| 79 | + if ($this->result->hasChanges()) { |
|
| 80 | 80 | $this->configuration->saveConfiguration(); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -91,18 +91,18 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function countEntries($filter, $type) { |
| 93 | 93 | $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
| 94 | - if($type === 'users') { |
|
| 94 | + if ($type === 'users') { |
|
| 95 | 95 | $reqs[] = 'ldapUserFilter'; |
| 96 | 96 | } |
| 97 | - if(!$this->checkRequirements($reqs)) { |
|
| 97 | + if (!$this->checkRequirements($reqs)) { |
|
| 98 | 98 | throw new \Exception('Requirements not met', 400); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | $attr = array('dn'); // default |
| 102 | 102 | $limit = 1001; |
| 103 | - if($type === 'groups') { |
|
| 104 | - $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 105 | - } else if($type === 'users') { |
|
| 103 | + if ($type === 'groups') { |
|
| 104 | + $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 105 | + } else if ($type === 'users') { |
|
| 106 | 106 | $result = $this->access->countUsers($filter, $attr, $limit); |
| 107 | 107 | } else if ($type === 'objects') { |
| 108 | 108 | $result = $this->access->countObjects($limit); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | private function formatCountResult($count) { |
| 124 | 124 | $formatted = ($count !== false) ? $count : 0; |
| 125 | - if($formatted > 1000) { |
|
| 125 | + if ($formatted > 1000) { |
|
| 126 | 126 | $formatted = '> 1000'; |
| 127 | 127 | } |
| 128 | 128 | return $formatted; |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | public function countGroups() { |
| 132 | 132 | $filter = $this->configuration->ldapGroupFilter; |
| 133 | 133 | |
| 134 | - if(empty($filter)) { |
|
| 134 | + if (empty($filter)) { |
|
| 135 | 135 | $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
| 136 | 136 | $this->result->addChange('ldap_group_count', $output); |
| 137 | 137 | return $this->result; |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
| 142 | 142 | } catch (\Exception $e) { |
| 143 | 143 | //400 can be ignored, 500 is forwarded |
| 144 | - if($e->getCode() === 500) { |
|
| 144 | + if ($e->getCode() === 500) { |
|
| 145 | 145 | throw $e; |
| 146 | 146 | } |
| 147 | 147 | return false; |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | public function countInBaseDN() { |
| 174 | 174 | // we don't need to provide a filter in this case |
| 175 | 175 | $total = $this->countEntries(null, 'objects'); |
| 176 | - if($total === false) { |
|
| 176 | + if ($total === false) { |
|
| 177 | 177 | throw new \Exception('invalid results received'); |
| 178 | 178 | } |
| 179 | 179 | $this->result->addChange('ldap_test_base', $total); |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | * @return int|bool |
| 188 | 188 | */ |
| 189 | 189 | public function countUsersWithAttribute($attr, $existsCheck = false) { |
| 190 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 190 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 191 | 191 | 'ldapPort', |
| 192 | 192 | 'ldapBase', |
| 193 | 193 | 'ldapUserFilter', |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | |
| 198 | 198 | $filter = $this->access->combineFilterWithAnd(array( |
| 199 | 199 | $this->configuration->ldapUserFilter, |
| 200 | - $attr . '=*' |
|
| 200 | + $attr.'=*' |
|
| 201 | 201 | )); |
| 202 | 202 | |
| 203 | 203 | $limit = ($existsCheck === false) ? null : 1; |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @throws \Exception |
| 213 | 213 | */ |
| 214 | 214 | public function detectUserDisplayNameAttribute() { |
| 215 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 215 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 216 | 216 | 'ldapPort', |
| 217 | 217 | 'ldapBase', |
| 218 | 218 | 'ldapUserFilter', |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | // most likely not the default value with upper case N, |
| 226 | 226 | // verify it still produces a result |
| 227 | 227 | $count = intval($this->countUsersWithAttribute($attr, true)); |
| 228 | - if($count > 0) { |
|
| 228 | + if ($count > 0) { |
|
| 229 | 229 | //no change, but we sent it back to make sure the user interface |
| 230 | 230 | //is still correct, even if the ajax call was cancelled meanwhile |
| 231 | 231 | $this->result->addChange('ldap_display_name', $attr); |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | foreach ($displayNameAttrs as $attr) { |
| 239 | 239 | $count = intval($this->countUsersWithAttribute($attr, true)); |
| 240 | 240 | |
| 241 | - if($count > 0) { |
|
| 241 | + if ($count > 0) { |
|
| 242 | 242 | $this->applyFind('ldap_display_name', $attr); |
| 243 | 243 | return $this->result; |
| 244 | 244 | } |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | * @return WizardResult|bool |
| 255 | 255 | */ |
| 256 | 256 | public function detectEmailAttribute() { |
| 257 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 257 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 258 | 258 | 'ldapPort', |
| 259 | 259 | 'ldapBase', |
| 260 | 260 | 'ldapUserFilter', |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | $attr = $this->configuration->ldapEmailAttribute; |
| 266 | 266 | if ($attr !== '') { |
| 267 | 267 | $count = intval($this->countUsersWithAttribute($attr, true)); |
| 268 | - if($count > 0) { |
|
| 268 | + if ($count > 0) { |
|
| 269 | 269 | return false; |
| 270 | 270 | } |
| 271 | 271 | $writeLog = true; |
@@ -276,19 +276,19 @@ discard block |
||
| 276 | 276 | $emailAttributes = array('mail', 'mailPrimaryAddress'); |
| 277 | 277 | $winner = ''; |
| 278 | 278 | $maxUsers = 0; |
| 279 | - foreach($emailAttributes as $attr) { |
|
| 279 | + foreach ($emailAttributes as $attr) { |
|
| 280 | 280 | $count = $this->countUsersWithAttribute($attr); |
| 281 | - if($count > $maxUsers) { |
|
| 281 | + if ($count > $maxUsers) { |
|
| 282 | 282 | $maxUsers = $count; |
| 283 | 283 | $winner = $attr; |
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | - if($winner !== '') { |
|
| 287 | + if ($winner !== '') { |
|
| 288 | 288 | $this->applyFind('ldap_email_attr', $winner); |
| 289 | - if($writeLog) { |
|
| 290 | - \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 291 | - 'automatically been reset, because the original value ' . |
|
| 289 | + if ($writeLog) { |
|
| 290 | + \OCP\Util::writeLog('user_ldap', 'The mail attribute has '. |
|
| 291 | + 'automatically been reset, because the original value '. |
|
| 292 | 292 | 'did not return any results.', \OCP\Util::INFO); |
| 293 | 293 | } |
| 294 | 294 | } |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @throws \Exception |
| 302 | 302 | */ |
| 303 | 303 | public function determineAttributes() { |
| 304 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 304 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 305 | 305 | 'ldapPort', |
| 306 | 306 | 'ldapBase', |
| 307 | 307 | 'ldapUserFilter', |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
| 318 | 318 | |
| 319 | 319 | $selected = $this->configuration->ldapLoginFilterAttributes; |
| 320 | - if(is_array($selected) && !empty($selected)) { |
|
| 320 | + if (is_array($selected) && !empty($selected)) { |
|
| 321 | 321 | $this->result->addChange('ldap_loginfilter_attributes', $selected); |
| 322 | 322 | } |
| 323 | 323 | |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | * @throws \Exception |
| 331 | 331 | */ |
| 332 | 332 | private function getUserAttributes() { |
| 333 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 333 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 334 | 334 | 'ldapPort', |
| 335 | 335 | 'ldapBase', |
| 336 | 336 | 'ldapUserFilter', |
@@ -338,20 +338,20 @@ discard block |
||
| 338 | 338 | return false; |
| 339 | 339 | } |
| 340 | 340 | $cr = $this->getConnection(); |
| 341 | - if(!$cr) { |
|
| 341 | + if (!$cr) { |
|
| 342 | 342 | throw new \Exception('Could not connect to LDAP'); |
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | $base = $this->configuration->ldapBase[0]; |
| 346 | 346 | $filter = $this->configuration->ldapUserFilter; |
| 347 | 347 | $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
| 348 | - if(!$this->ldap->isResource($rr)) { |
|
| 348 | + if (!$this->ldap->isResource($rr)) { |
|
| 349 | 349 | return false; |
| 350 | 350 | } |
| 351 | 351 | $er = $this->ldap->firstEntry($cr, $rr); |
| 352 | 352 | $attributes = $this->ldap->getAttributes($cr, $er); |
| 353 | 353 | $pureAttributes = array(); |
| 354 | - for($i = 0; $i < $attributes['count']; $i++) { |
|
| 354 | + for ($i = 0; $i < $attributes['count']; $i++) { |
|
| 355 | 355 | $pureAttributes[] = $attributes[$i]; |
| 356 | 356 | } |
| 357 | 357 | |
@@ -386,23 +386,23 @@ discard block |
||
| 386 | 386 | * @throws \Exception |
| 387 | 387 | */ |
| 388 | 388 | private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
| 389 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 389 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 390 | 390 | 'ldapPort', |
| 391 | 391 | 'ldapBase', |
| 392 | 392 | ))) { |
| 393 | 393 | return false; |
| 394 | 394 | } |
| 395 | 395 | $cr = $this->getConnection(); |
| 396 | - if(!$cr) { |
|
| 396 | + if (!$cr) { |
|
| 397 | 397 | throw new \Exception('Could not connect to LDAP'); |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | $this->fetchGroups($dbKey, $confKey); |
| 401 | 401 | |
| 402 | - if($testMemberOf) { |
|
| 402 | + if ($testMemberOf) { |
|
| 403 | 403 | $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
| 404 | 404 | $this->result->markChange(); |
| 405 | - if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 405 | + if (!$this->configuration->hasMemberOfFilterSupport) { |
|
| 406 | 406 | throw new \Exception('memberOf is not supported by the server'); |
| 407 | 407 | } |
| 408 | 408 | } |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
| 423 | 423 | |
| 424 | 424 | $filterParts = array(); |
| 425 | - foreach($obclasses as $obclass) { |
|
| 425 | + foreach ($obclasses as $obclass) { |
|
| 426 | 426 | $filterParts[] = 'objectclass='.$obclass; |
| 427 | 427 | } |
| 428 | 428 | //we filter for everything |
@@ -439,8 +439,8 @@ discard block |
||
| 439 | 439 | // we need to request dn additionally here, otherwise memberOf |
| 440 | 440 | // detection will fail later |
| 441 | 441 | $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
| 442 | - foreach($result as $item) { |
|
| 443 | - if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 442 | + foreach ($result as $item) { |
|
| 443 | + if (!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 444 | 444 | // just in case - no issue known |
| 445 | 445 | continue; |
| 446 | 446 | } |
@@ -450,7 +450,7 @@ discard block |
||
| 450 | 450 | $offset += $limit; |
| 451 | 451 | } while ($this->access->hasMoreResults()); |
| 452 | 452 | |
| 453 | - if(count($groupNames) > 0) { |
|
| 453 | + if (count($groupNames) > 0) { |
|
| 454 | 454 | natsort($groupNames); |
| 455 | 455 | $this->result->addOptions($dbKey, array_values($groupNames)); |
| 456 | 456 | } else { |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | $setFeatures = $this->configuration->$confKey; |
| 461 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 461 | + if (is_array($setFeatures) && !empty($setFeatures)) { |
|
| 462 | 462 | //something is already configured? pre-select it. |
| 463 | 463 | $this->result->addChange($dbKey, $setFeatures); |
| 464 | 464 | } |
@@ -466,14 +466,14 @@ discard block |
||
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | public function determineGroupMemberAssoc() { |
| 469 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 469 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 470 | 470 | 'ldapPort', |
| 471 | 471 | 'ldapGroupFilter', |
| 472 | 472 | ))) { |
| 473 | 473 | return false; |
| 474 | 474 | } |
| 475 | 475 | $attribute = $this->detectGroupMemberAssoc(); |
| 476 | - if($attribute === false) { |
|
| 476 | + if ($attribute === false) { |
|
| 477 | 477 | return false; |
| 478 | 478 | } |
| 479 | 479 | $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
@@ -488,14 +488,14 @@ discard block |
||
| 488 | 488 | * @throws \Exception |
| 489 | 489 | */ |
| 490 | 490 | public function determineGroupObjectClasses() { |
| 491 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 491 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 492 | 492 | 'ldapPort', |
| 493 | 493 | 'ldapBase', |
| 494 | 494 | ))) { |
| 495 | 495 | return false; |
| 496 | 496 | } |
| 497 | 497 | $cr = $this->getConnection(); |
| 498 | - if(!$cr) { |
|
| 498 | + if (!$cr) { |
|
| 499 | 499 | throw new \Exception('Could not connect to LDAP'); |
| 500 | 500 | } |
| 501 | 501 | |
@@ -515,14 +515,14 @@ discard block |
||
| 515 | 515 | * @throws \Exception |
| 516 | 516 | */ |
| 517 | 517 | public function determineUserObjectClasses() { |
| 518 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 518 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 519 | 519 | 'ldapPort', |
| 520 | 520 | 'ldapBase', |
| 521 | 521 | ))) { |
| 522 | 522 | return false; |
| 523 | 523 | } |
| 524 | 524 | $cr = $this->getConnection(); |
| 525 | - if(!$cr) { |
|
| 525 | + if (!$cr) { |
|
| 526 | 526 | throw new \Exception('Could not connect to LDAP'); |
| 527 | 527 | } |
| 528 | 528 | |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | * @throws \Exception |
| 546 | 546 | */ |
| 547 | 547 | public function getGroupFilter() { |
| 548 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 548 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 549 | 549 | 'ldapPort', |
| 550 | 550 | 'ldapBase', |
| 551 | 551 | ))) { |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | * @throws \Exception |
| 570 | 570 | */ |
| 571 | 571 | public function getUserListFilter() { |
| 572 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 572 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 573 | 573 | 'ldapPort', |
| 574 | 574 | 'ldapBase', |
| 575 | 575 | ))) { |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
| 583 | 583 | } |
| 584 | 584 | $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
| 585 | - if(!$filter) { |
|
| 585 | + if (!$filter) { |
|
| 586 | 586 | throw new \Exception('Cannot create filter'); |
| 587 | 587 | } |
| 588 | 588 | |
@@ -595,7 +595,7 @@ discard block |
||
| 595 | 595 | * @throws \Exception |
| 596 | 596 | */ |
| 597 | 597 | public function getUserLoginFilter() { |
| 598 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 598 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 599 | 599 | 'ldapPort', |
| 600 | 600 | 'ldapBase', |
| 601 | 601 | 'ldapUserFilter', |
@@ -604,7 +604,7 @@ discard block |
||
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
| 607 | - if(!$filter) { |
|
| 607 | + if (!$filter) { |
|
| 608 | 608 | throw new \Exception('Cannot create filter'); |
| 609 | 609 | } |
| 610 | 610 | |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | * @throws \Exception |
| 619 | 619 | */ |
| 620 | 620 | public function testLoginName($loginName) { |
| 621 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 621 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 622 | 622 | 'ldapPort', |
| 623 | 623 | 'ldapBase', |
| 624 | 624 | 'ldapLoginFilter', |
@@ -627,17 +627,17 @@ discard block |
||
| 627 | 627 | } |
| 628 | 628 | |
| 629 | 629 | $cr = $this->access->connection->getConnectionResource(); |
| 630 | - if(!$this->ldap->isResource($cr)) { |
|
| 630 | + if (!$this->ldap->isResource($cr)) { |
|
| 631 | 631 | throw new \Exception('connection error'); |
| 632 | 632 | } |
| 633 | 633 | |
| 634 | - if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 634 | + if (mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 635 | 635 | === false) { |
| 636 | 636 | throw new \Exception('missing placeholder'); |
| 637 | 637 | } |
| 638 | 638 | |
| 639 | 639 | $users = $this->access->countUsersByLoginName($loginName); |
| 640 | - if($this->ldap->errno($cr) !== 0) { |
|
| 640 | + if ($this->ldap->errno($cr) !== 0) { |
|
| 641 | 641 | throw new \Exception($this->ldap->error($cr)); |
| 642 | 642 | } |
| 643 | 643 | $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
@@ -652,22 +652,22 @@ discard block |
||
| 652 | 652 | * @throws \Exception |
| 653 | 653 | */ |
| 654 | 654 | public function guessPortAndTLS() { |
| 655 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 655 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 656 | 656 | ))) { |
| 657 | 657 | return false; |
| 658 | 658 | } |
| 659 | 659 | $this->checkHost(); |
| 660 | 660 | $portSettings = $this->getPortSettingsToTry(); |
| 661 | 661 | |
| 662 | - if(!is_array($portSettings)) { |
|
| 662 | + if (!is_array($portSettings)) { |
|
| 663 | 663 | throw new \Exception(print_r($portSettings, true)); |
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | //proceed from the best configuration and return on first success |
| 667 | - foreach($portSettings as $setting) { |
|
| 667 | + foreach ($portSettings as $setting) { |
|
| 668 | 668 | $p = $setting['port']; |
| 669 | 669 | $t = $setting['tls']; |
| 670 | - \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 670 | + \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '.$p.', TLS '.$t, \OCP\Util::DEBUG); |
|
| 671 | 671 | //connectAndBind may throw Exception, it needs to be catched by the |
| 672 | 672 | //callee of this method |
| 673 | 673 | |
@@ -677,7 +677,7 @@ discard block |
||
| 677 | 677 | // any reply other than -1 (= cannot connect) is already okay, |
| 678 | 678 | // because then we found the server |
| 679 | 679 | // unavailable startTLS returns -11 |
| 680 | - if($e->getCode() > 0) { |
|
| 680 | + if ($e->getCode() > 0) { |
|
| 681 | 681 | $settingsFound = true; |
| 682 | 682 | } else { |
| 683 | 683 | throw $e; |
@@ -690,7 +690,7 @@ discard block |
||
| 690 | 690 | 'ldapTLS' => intval($t) |
| 691 | 691 | ); |
| 692 | 692 | $this->configuration->setConfiguration($config); |
| 693 | - \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 693 | + \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port '.$p, \OCP\Util::DEBUG); |
|
| 694 | 694 | $this->result->addChange('ldap_port', $p); |
| 695 | 695 | return $this->result; |
| 696 | 696 | } |
@@ -705,7 +705,7 @@ discard block |
||
| 705 | 705 | * @return WizardResult|false WizardResult on success, false otherwise |
| 706 | 706 | */ |
| 707 | 707 | public function guessBaseDN() { |
| 708 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 708 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 709 | 709 | 'ldapPort', |
| 710 | 710 | ))) { |
| 711 | 711 | return false; |
@@ -714,9 +714,9 @@ discard block |
||
| 714 | 714 | //check whether a DN is given in the agent name (99.9% of all cases) |
| 715 | 715 | $base = null; |
| 716 | 716 | $i = stripos($this->configuration->ldapAgentName, 'dc='); |
| 717 | - if($i !== false) { |
|
| 717 | + if ($i !== false) { |
|
| 718 | 718 | $base = substr($this->configuration->ldapAgentName, $i); |
| 719 | - if($this->testBaseDN($base)) { |
|
| 719 | + if ($this->testBaseDN($base)) { |
|
| 720 | 720 | $this->applyFind('ldap_base', $base); |
| 721 | 721 | return $this->result; |
| 722 | 722 | } |
@@ -727,13 +727,13 @@ discard block |
||
| 727 | 727 | //a base DN |
| 728 | 728 | $helper = new Helper(\OC::$server->getConfig()); |
| 729 | 729 | $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
| 730 | - if(!$domain) { |
|
| 730 | + if (!$domain) { |
|
| 731 | 731 | return false; |
| 732 | 732 | } |
| 733 | 733 | |
| 734 | 734 | $dparts = explode('.', $domain); |
| 735 | - while(count($dparts) > 0) { |
|
| 736 | - $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 735 | + while (count($dparts) > 0) { |
|
| 736 | + $base2 = 'dc='.implode(',dc=', $dparts); |
|
| 737 | 737 | if ($base !== $base2 && $this->testBaseDN($base2)) { |
| 738 | 738 | $this->applyFind('ldap_base', $base2); |
| 739 | 739 | return $this->result; |
@@ -766,7 +766,7 @@ discard block |
||
| 766 | 766 | $hostInfo = parse_url($host); |
| 767 | 767 | |
| 768 | 768 | //removes Port from Host |
| 769 | - if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 769 | + if (is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 770 | 770 | $port = $hostInfo['port']; |
| 771 | 771 | $host = str_replace(':'.$port, '', $host); |
| 772 | 772 | $this->applyFind('ldap_host', $host); |
@@ -783,30 +783,30 @@ discard block |
||
| 783 | 783 | private function detectGroupMemberAssoc() { |
| 784 | 784 | $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
| 785 | 785 | $filter = $this->configuration->ldapGroupFilter; |
| 786 | - if(empty($filter)) { |
|
| 786 | + if (empty($filter)) { |
|
| 787 | 787 | return false; |
| 788 | 788 | } |
| 789 | 789 | $cr = $this->getConnection(); |
| 790 | - if(!$cr) { |
|
| 790 | + if (!$cr) { |
|
| 791 | 791 | throw new \Exception('Could not connect to LDAP'); |
| 792 | 792 | } |
| 793 | 793 | $base = $this->configuration->ldapBase[0]; |
| 794 | 794 | $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
| 795 | - if(!$this->ldap->isResource($rr)) { |
|
| 795 | + if (!$this->ldap->isResource($rr)) { |
|
| 796 | 796 | return false; |
| 797 | 797 | } |
| 798 | 798 | $er = $this->ldap->firstEntry($cr, $rr); |
| 799 | - while(is_resource($er)) { |
|
| 799 | + while (is_resource($er)) { |
|
| 800 | 800 | $this->ldap->getDN($cr, $er); |
| 801 | 801 | $attrs = $this->ldap->getAttributes($cr, $er); |
| 802 | 802 | $result = array(); |
| 803 | 803 | $possibleAttrsCount = count($possibleAttrs); |
| 804 | - for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 805 | - if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 804 | + for ($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 805 | + if (isset($attrs[$possibleAttrs[$i]])) { |
|
| 806 | 806 | $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
| 807 | 807 | } |
| 808 | 808 | } |
| 809 | - if(!empty($result)) { |
|
| 809 | + if (!empty($result)) { |
|
| 810 | 810 | natsort($result); |
| 811 | 811 | return key($result); |
| 812 | 812 | } |
@@ -825,14 +825,14 @@ discard block |
||
| 825 | 825 | */ |
| 826 | 826 | private function testBaseDN($base) { |
| 827 | 827 | $cr = $this->getConnection(); |
| 828 | - if(!$cr) { |
|
| 828 | + if (!$cr) { |
|
| 829 | 829 | throw new \Exception('Could not connect to LDAP'); |
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | //base is there, let's validate it. If we search for anything, we should |
| 833 | 833 | //get a result set > 0 on a proper base |
| 834 | 834 | $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
| 835 | - if(!$this->ldap->isResource($rr)) { |
|
| 835 | + if (!$this->ldap->isResource($rr)) { |
|
| 836 | 836 | $errorNo = $this->ldap->errno($cr); |
| 837 | 837 | $errorMsg = $this->ldap->error($cr); |
| 838 | 838 | \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
@@ -854,11 +854,11 @@ discard block |
||
| 854 | 854 | */ |
| 855 | 855 | private function testMemberOf() { |
| 856 | 856 | $cr = $this->getConnection(); |
| 857 | - if(!$cr) { |
|
| 857 | + if (!$cr) { |
|
| 858 | 858 | throw new \Exception('Could not connect to LDAP'); |
| 859 | 859 | } |
| 860 | 860 | $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
| 861 | - if(is_int($result) && $result > 0) { |
|
| 861 | + if (is_int($result) && $result > 0) { |
|
| 862 | 862 | return true; |
| 863 | 863 | } |
| 864 | 864 | return false; |
@@ -879,27 +879,27 @@ discard block |
||
| 879 | 879 | case self::LFILTER_USER_LIST: |
| 880 | 880 | $objcs = $this->configuration->ldapUserFilterObjectclass; |
| 881 | 881 | //glue objectclasses |
| 882 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 882 | + if (is_array($objcs) && count($objcs) > 0) { |
|
| 883 | 883 | $filter .= '(|'; |
| 884 | - foreach($objcs as $objc) { |
|
| 885 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 884 | + foreach ($objcs as $objc) { |
|
| 885 | + $filter .= '(objectclass='.$objc.')'; |
|
| 886 | 886 | } |
| 887 | 887 | $filter .= ')'; |
| 888 | 888 | $parts++; |
| 889 | 889 | } |
| 890 | 890 | //glue group memberships |
| 891 | - if($this->configuration->hasMemberOfFilterSupport) { |
|
| 891 | + if ($this->configuration->hasMemberOfFilterSupport) { |
|
| 892 | 892 | $cns = $this->configuration->ldapUserFilterGroups; |
| 893 | - if(is_array($cns) && count($cns) > 0) { |
|
| 893 | + if (is_array($cns) && count($cns) > 0) { |
|
| 894 | 894 | $filter .= '(|'; |
| 895 | 895 | $cr = $this->getConnection(); |
| 896 | - if(!$cr) { |
|
| 896 | + if (!$cr) { |
|
| 897 | 897 | throw new \Exception('Could not connect to LDAP'); |
| 898 | 898 | } |
| 899 | 899 | $base = $this->configuration->ldapBase[0]; |
| 900 | - foreach($cns as $cn) { |
|
| 901 | - $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 902 | - if(!$this->ldap->isResource($rr)) { |
|
| 900 | + foreach ($cns as $cn) { |
|
| 901 | + $rr = $this->ldap->search($cr, $base, 'cn='.$cn, array('dn', 'primaryGroupToken')); |
|
| 902 | + if (!$this->ldap->isResource($rr)) { |
|
| 903 | 903 | continue; |
| 904 | 904 | } |
| 905 | 905 | $er = $this->ldap->firstEntry($cr, $rr); |
@@ -908,11 +908,11 @@ discard block |
||
| 908 | 908 | if ($dn == false || $dn === '') { |
| 909 | 909 | continue; |
| 910 | 910 | } |
| 911 | - $filterPart = '(memberof=' . $dn . ')'; |
|
| 912 | - if(isset($attrs['primaryGroupToken'])) { |
|
| 911 | + $filterPart = '(memberof='.$dn.')'; |
|
| 912 | + if (isset($attrs['primaryGroupToken'])) { |
|
| 913 | 913 | $pgt = $attrs['primaryGroupToken'][0]; |
| 914 | - $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 915 | - $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 914 | + $primaryFilterPart = '(primaryGroupID='.$pgt.')'; |
|
| 915 | + $filterPart = '(|'.$filterPart.$primaryFilterPart.')'; |
|
| 916 | 916 | } |
| 917 | 917 | $filter .= $filterPart; |
| 918 | 918 | } |
@@ -921,8 +921,8 @@ discard block |
||
| 921 | 921 | $parts++; |
| 922 | 922 | } |
| 923 | 923 | //wrap parts in AND condition |
| 924 | - if($parts > 1) { |
|
| 925 | - $filter = '(&' . $filter . ')'; |
|
| 924 | + if ($parts > 1) { |
|
| 925 | + $filter = '(&'.$filter.')'; |
|
| 926 | 926 | } |
| 927 | 927 | if ($filter === '') { |
| 928 | 928 | $filter = '(objectclass=*)'; |
@@ -932,27 +932,27 @@ discard block |
||
| 932 | 932 | case self::LFILTER_GROUP_LIST: |
| 933 | 933 | $objcs = $this->configuration->ldapGroupFilterObjectclass; |
| 934 | 934 | //glue objectclasses |
| 935 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 935 | + if (is_array($objcs) && count($objcs) > 0) { |
|
| 936 | 936 | $filter .= '(|'; |
| 937 | - foreach($objcs as $objc) { |
|
| 938 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 937 | + foreach ($objcs as $objc) { |
|
| 938 | + $filter .= '(objectclass='.$objc.')'; |
|
| 939 | 939 | } |
| 940 | 940 | $filter .= ')'; |
| 941 | 941 | $parts++; |
| 942 | 942 | } |
| 943 | 943 | //glue group memberships |
| 944 | 944 | $cns = $this->configuration->ldapGroupFilterGroups; |
| 945 | - if(is_array($cns) && count($cns) > 0) { |
|
| 945 | + if (is_array($cns) && count($cns) > 0) { |
|
| 946 | 946 | $filter .= '(|'; |
| 947 | - foreach($cns as $cn) { |
|
| 948 | - $filter .= '(cn=' . $cn . ')'; |
|
| 947 | + foreach ($cns as $cn) { |
|
| 948 | + $filter .= '(cn='.$cn.')'; |
|
| 949 | 949 | } |
| 950 | 950 | $filter .= ')'; |
| 951 | 951 | } |
| 952 | 952 | $parts++; |
| 953 | 953 | //wrap parts in AND condition |
| 954 | - if($parts > 1) { |
|
| 955 | - $filter = '(&' . $filter . ')'; |
|
| 954 | + if ($parts > 1) { |
|
| 955 | + $filter = '(&'.$filter.')'; |
|
| 956 | 956 | } |
| 957 | 957 | break; |
| 958 | 958 | |
@@ -964,47 +964,47 @@ discard block |
||
| 964 | 964 | $userAttributes = array_change_key_case(array_flip($userAttributes)); |
| 965 | 965 | $parts = 0; |
| 966 | 966 | |
| 967 | - if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 967 | + if ($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 968 | 968 | $attr = ''; |
| 969 | - if(isset($userAttributes['uid'])) { |
|
| 969 | + if (isset($userAttributes['uid'])) { |
|
| 970 | 970 | $attr = 'uid'; |
| 971 | - } else if(isset($userAttributes['samaccountname'])) { |
|
| 971 | + } else if (isset($userAttributes['samaccountname'])) { |
|
| 972 | 972 | $attr = 'samaccountname'; |
| 973 | - } else if(isset($userAttributes['cn'])) { |
|
| 973 | + } else if (isset($userAttributes['cn'])) { |
|
| 974 | 974 | //fallback |
| 975 | 975 | $attr = 'cn'; |
| 976 | 976 | } |
| 977 | 977 | if ($attr !== '') { |
| 978 | - $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 978 | + $filterUsername = '('.$attr.$loginpart.')'; |
|
| 979 | 979 | $parts++; |
| 980 | 980 | } |
| 981 | 981 | } |
| 982 | 982 | |
| 983 | 983 | $filterEmail = ''; |
| 984 | - if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 984 | + if ($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 985 | 985 | $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
| 986 | 986 | $parts++; |
| 987 | 987 | } |
| 988 | 988 | |
| 989 | 989 | $filterAttributes = ''; |
| 990 | 990 | $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
| 991 | - if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 991 | + if (is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 992 | 992 | $filterAttributes = '(|'; |
| 993 | - foreach($attrsToFilter as $attribute) { |
|
| 994 | - $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 993 | + foreach ($attrsToFilter as $attribute) { |
|
| 994 | + $filterAttributes .= '('.$attribute.$loginpart.')'; |
|
| 995 | 995 | } |
| 996 | 996 | $filterAttributes .= ')'; |
| 997 | 997 | $parts++; |
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | $filterLogin = ''; |
| 1001 | - if($parts > 1) { |
|
| 1001 | + if ($parts > 1) { |
|
| 1002 | 1002 | $filterLogin = '(|'; |
| 1003 | 1003 | } |
| 1004 | 1004 | $filterLogin .= $filterUsername; |
| 1005 | 1005 | $filterLogin .= $filterEmail; |
| 1006 | 1006 | $filterLogin .= $filterAttributes; |
| 1007 | - if($parts > 1) { |
|
| 1007 | + if ($parts > 1) { |
|
| 1008 | 1008 | $filterLogin .= ')'; |
| 1009 | 1009 | } |
| 1010 | 1010 | |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | * @throws \Exception |
| 1027 | 1027 | */ |
| 1028 | 1028 | private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
| 1029 | - if($ncc) { |
|
| 1029 | + if ($ncc) { |
|
| 1030 | 1030 | $originalLDAPTLS_REQCERT = strval(getenv('LDAPTLS_REQCERT')); |
| 1031 | 1031 | //No certificate check |
| 1032 | 1032 | putenv('LDAPTLS_REQCERT=never'); |
@@ -1036,12 +1036,12 @@ discard block |
||
| 1036 | 1036 | \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
| 1037 | 1037 | $host = $this->configuration->ldapHost; |
| 1038 | 1038 | $hostInfo = parse_url($host); |
| 1039 | - if(!$hostInfo) { |
|
| 1039 | + if (!$hostInfo) { |
|
| 1040 | 1040 | throw new \Exception(self::$l->t('Invalid Host')); |
| 1041 | 1041 | } |
| 1042 | 1042 | \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
| 1043 | 1043 | $cr = $this->ldap->connect($host, $port); |
| 1044 | - if(!is_resource($cr)) { |
|
| 1044 | + if (!is_resource($cr)) { |
|
| 1045 | 1045 | throw new \Exception(self::$l->t('Invalid Host')); |
| 1046 | 1046 | } |
| 1047 | 1047 | |
@@ -1052,9 +1052,9 @@ discard block |
||
| 1052 | 1052 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
| 1053 | 1053 | |
| 1054 | 1054 | try { |
| 1055 | - if($tls) { |
|
| 1055 | + if ($tls) { |
|
| 1056 | 1056 | $isTlsWorking = @$this->ldap->startTls($cr); |
| 1057 | - if(!$isTlsWorking) { |
|
| 1057 | + if (!$isTlsWorking) { |
|
| 1058 | 1058 | return false; |
| 1059 | 1059 | } |
| 1060 | 1060 | } |
@@ -1068,27 +1068,27 @@ discard block |
||
| 1068 | 1068 | $errNo = $this->ldap->errno($cr); |
| 1069 | 1069 | $error = ldap_error($cr); |
| 1070 | 1070 | $this->ldap->unbind($cr); |
| 1071 | - } catch(ServerNotAvailableException $e) { |
|
| 1072 | - if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1073 | - putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1071 | + } catch (ServerNotAvailableException $e) { |
|
| 1072 | + if ($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1073 | + putenv('LDAPTLS_REQCERT='.$originalLDAPTLS_REQCERT); |
|
| 1074 | 1074 | } |
| 1075 | 1075 | return false; |
| 1076 | 1076 | } |
| 1077 | 1077 | |
| 1078 | - if($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1079 | - putenv('LDAPTLS_REQCERT=' . $originalLDAPTLS_REQCERT); |
|
| 1078 | + if ($ncc && isset($originalLDAPTLS_REQCERT)) { |
|
| 1079 | + putenv('LDAPTLS_REQCERT='.$originalLDAPTLS_REQCERT); |
|
| 1080 | 1080 | } |
| 1081 | 1081 | |
| 1082 | - if($login === true) { |
|
| 1082 | + if ($login === true) { |
|
| 1083 | 1083 | $this->ldap->unbind($cr); |
| 1084 | - if($ncc) { |
|
| 1084 | + if ($ncc) { |
|
| 1085 | 1085 | throw new \Exception('Certificate cannot be validated.'); |
| 1086 | 1086 | } |
| 1087 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
|
| 1087 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '.$port.' TLS '.intval($tls), \OCP\Util::DEBUG); |
|
| 1088 | 1088 | return true; |
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | - if($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
| 1091 | + if ($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
| 1092 | 1092 | //host, port or TLS wrong |
| 1093 | 1093 | return false; |
| 1094 | 1094 | } else if ($errNo === 2) { |
@@ -1118,9 +1118,9 @@ discard block |
||
| 1118 | 1118 | */ |
| 1119 | 1119 | private function checkRequirements($reqs) { |
| 1120 | 1120 | $this->checkAgentRequirements(); |
| 1121 | - foreach($reqs as $option) { |
|
| 1121 | + foreach ($reqs as $option) { |
|
| 1122 | 1122 | $value = $this->configuration->$option; |
| 1123 | - if(empty($value)) { |
|
| 1123 | + if (empty($value)) { |
|
| 1124 | 1124 | return false; |
| 1125 | 1125 | } |
| 1126 | 1126 | } |
@@ -1142,33 +1142,33 @@ discard block |
||
| 1142 | 1142 | $dnRead = array(); |
| 1143 | 1143 | $foundItems = array(); |
| 1144 | 1144 | $maxEntries = 0; |
| 1145 | - if(!is_array($this->configuration->ldapBase) |
|
| 1145 | + if (!is_array($this->configuration->ldapBase) |
|
| 1146 | 1146 | || !isset($this->configuration->ldapBase[0])) { |
| 1147 | 1147 | return false; |
| 1148 | 1148 | } |
| 1149 | 1149 | $base = $this->configuration->ldapBase[0]; |
| 1150 | 1150 | $cr = $this->getConnection(); |
| 1151 | - if(!$this->ldap->isResource($cr)) { |
|
| 1151 | + if (!$this->ldap->isResource($cr)) { |
|
| 1152 | 1152 | return false; |
| 1153 | 1153 | } |
| 1154 | 1154 | $lastFilter = null; |
| 1155 | - if(isset($filters[count($filters)-1])) { |
|
| 1156 | - $lastFilter = $filters[count($filters)-1]; |
|
| 1155 | + if (isset($filters[count($filters) - 1])) { |
|
| 1156 | + $lastFilter = $filters[count($filters) - 1]; |
|
| 1157 | 1157 | } |
| 1158 | - foreach($filters as $filter) { |
|
| 1159 | - if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1158 | + foreach ($filters as $filter) { |
|
| 1159 | + if ($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1160 | 1160 | //skip when the filter is a wildcard and results were found |
| 1161 | 1161 | continue; |
| 1162 | 1162 | } |
| 1163 | 1163 | // 20k limit for performance and reason |
| 1164 | 1164 | $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
| 1165 | - if(!$this->ldap->isResource($rr)) { |
|
| 1165 | + if (!$this->ldap->isResource($rr)) { |
|
| 1166 | 1166 | continue; |
| 1167 | 1167 | } |
| 1168 | 1168 | $entries = $this->ldap->countEntries($cr, $rr); |
| 1169 | 1169 | $getEntryFunc = 'firstEntry'; |
| 1170 | - if(($entries !== false) && ($entries > 0)) { |
|
| 1171 | - if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1170 | + if (($entries !== false) && ($entries > 0)) { |
|
| 1171 | + if (!is_null($maxF) && $entries > $maxEntries) { |
|
| 1172 | 1172 | $maxEntries = $entries; |
| 1173 | 1173 | $maxF = $filter; |
| 1174 | 1174 | } |
@@ -1176,13 +1176,13 @@ discard block |
||
| 1176 | 1176 | do { |
| 1177 | 1177 | $entry = $this->ldap->$getEntryFunc($cr, $rr); |
| 1178 | 1178 | $getEntryFunc = 'nextEntry'; |
| 1179 | - if(!$this->ldap->isResource($entry)) { |
|
| 1179 | + if (!$this->ldap->isResource($entry)) { |
|
| 1180 | 1180 | continue 2; |
| 1181 | 1181 | } |
| 1182 | 1182 | $rr = $entry; //will be expected by nextEntry next round |
| 1183 | 1183 | $attributes = $this->ldap->getAttributes($cr, $entry); |
| 1184 | 1184 | $dn = $this->ldap->getDN($cr, $entry); |
| 1185 | - if($dn === false || in_array($dn, $dnRead)) { |
|
| 1185 | + if ($dn === false || in_array($dn, $dnRead)) { |
|
| 1186 | 1186 | continue; |
| 1187 | 1187 | } |
| 1188 | 1188 | $newItems = array(); |
@@ -1193,7 +1193,7 @@ discard block |
||
| 1193 | 1193 | $foundItems = array_merge($foundItems, $newItems); |
| 1194 | 1194 | $this->resultCache[$dn][$attr] = $newItems; |
| 1195 | 1195 | $dnRead[] = $dn; |
| 1196 | - } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1196 | + } while (($state === self::LRESULT_PROCESSED_SKIP |
|
| 1197 | 1197 | || $this->ldap->isResource($entry)) |
| 1198 | 1198 | && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
| 1199 | 1199 | } |
@@ -1216,11 +1216,11 @@ discard block |
||
| 1216 | 1216 | */ |
| 1217 | 1217 | private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
| 1218 | 1218 | $cr = $this->getConnection(); |
| 1219 | - if(!$cr) { |
|
| 1219 | + if (!$cr) { |
|
| 1220 | 1220 | throw new \Exception('Could not connect to LDAP'); |
| 1221 | 1221 | } |
| 1222 | 1222 | $p = 'objectclass='; |
| 1223 | - foreach($objectclasses as $key => $value) { |
|
| 1223 | + foreach ($objectclasses as $key => $value) { |
|
| 1224 | 1224 | $objectclasses[$key] = $p.$value; |
| 1225 | 1225 | } |
| 1226 | 1226 | $maxEntryObjC = ''; |
@@ -1232,7 +1232,7 @@ discard block |
||
| 1232 | 1232 | $availableFeatures = |
| 1233 | 1233 | $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
| 1234 | 1234 | $dig, $maxEntryObjC); |
| 1235 | - if(is_array($availableFeatures) |
|
| 1235 | + if (is_array($availableFeatures) |
|
| 1236 | 1236 | && count($availableFeatures) > 0) { |
| 1237 | 1237 | natcasesort($availableFeatures); |
| 1238 | 1238 | //natcasesort keeps indices, but we must get rid of them for proper |
@@ -1243,7 +1243,7 @@ discard block |
||
| 1243 | 1243 | } |
| 1244 | 1244 | |
| 1245 | 1245 | $setFeatures = $this->configuration->$confkey; |
| 1246 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1246 | + if (is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1247 | 1247 | //something is already configured? pre-select it. |
| 1248 | 1248 | $this->result->addChange($dbkey, $setFeatures); |
| 1249 | 1249 | } else if ($po && $maxEntryObjC !== '') { |
@@ -1265,7 +1265,7 @@ discard block |
||
| 1265 | 1265 | * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
| 1266 | 1266 | */ |
| 1267 | 1267 | private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
| 1268 | - if(!is_array($result) |
|
| 1268 | + if (!is_array($result) |
|
| 1269 | 1269 | || !isset($result['count']) |
| 1270 | 1270 | || !$result['count'] > 0) { |
| 1271 | 1271 | return self::LRESULT_PROCESSED_INVALID; |
@@ -1274,12 +1274,12 @@ discard block |
||
| 1274 | 1274 | // strtolower on all keys for proper comparison |
| 1275 | 1275 | $result = \OCP\Util::mb_array_change_key_case($result); |
| 1276 | 1276 | $attribute = strtolower($attribute); |
| 1277 | - if(isset($result[$attribute])) { |
|
| 1278 | - foreach($result[$attribute] as $key => $val) { |
|
| 1279 | - if($key === 'count') { |
|
| 1277 | + if (isset($result[$attribute])) { |
|
| 1278 | + foreach ($result[$attribute] as $key => $val) { |
|
| 1279 | + if ($key === 'count') { |
|
| 1280 | 1280 | continue; |
| 1281 | 1281 | } |
| 1282 | - if(!in_array($val, $known)) { |
|
| 1282 | + if (!in_array($val, $known)) { |
|
| 1283 | 1283 | $known[] = $val; |
| 1284 | 1284 | } |
| 1285 | 1285 | } |
@@ -1293,7 +1293,7 @@ discard block |
||
| 1293 | 1293 | * @return bool|mixed |
| 1294 | 1294 | */ |
| 1295 | 1295 | private function getConnection() { |
| 1296 | - if(!is_null($this->cr)) { |
|
| 1296 | + if (!is_null($this->cr)) { |
|
| 1297 | 1297 | return $this->cr; |
| 1298 | 1298 | } |
| 1299 | 1299 | |
@@ -1305,14 +1305,14 @@ discard block |
||
| 1305 | 1305 | $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
| 1306 | 1306 | $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
| 1307 | 1307 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
| 1308 | - if($this->configuration->ldapTLS === 1) { |
|
| 1308 | + if ($this->configuration->ldapTLS === 1) { |
|
| 1309 | 1309 | $this->ldap->startTls($cr); |
| 1310 | 1310 | } |
| 1311 | 1311 | |
| 1312 | 1312 | $lo = @$this->ldap->bind($cr, |
| 1313 | 1313 | $this->configuration->ldapAgentName, |
| 1314 | 1314 | $this->configuration->ldapAgentPassword); |
| 1315 | - if($lo === true) { |
|
| 1315 | + if ($lo === true) { |
|
| 1316 | 1316 | $this->$cr = $cr; |
| 1317 | 1317 | return $cr; |
| 1318 | 1318 | } |
@@ -1347,14 +1347,14 @@ discard block |
||
| 1347 | 1347 | $portSettings = array(); |
| 1348 | 1348 | |
| 1349 | 1349 | //In case the port is already provided, we will check this first |
| 1350 | - if($port > 0) { |
|
| 1350 | + if ($port > 0) { |
|
| 1351 | 1351 | $hostInfo = parse_url($host); |
| 1352 | - if(!(is_array($hostInfo) |
|
| 1352 | + if (!(is_array($hostInfo) |
|
| 1353 | 1353 | && isset($hostInfo['scheme']) |
| 1354 | 1354 | && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
| 1355 | 1355 | $portSettings[] = array('port' => $port, 'tls' => true); |
| 1356 | 1356 | } |
| 1357 | - $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1357 | + $portSettings[] = array('port' => $port, 'tls' => false); |
|
| 1358 | 1358 | } |
| 1359 | 1359 | |
| 1360 | 1360 | //default ports |