testConfiguration.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Allan Nordhøy <epost@anotheragency.no>
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. // Check user and app status
  30. \OC_JSON::checkAdminUser();
  31. \OC_JSON::checkAppEnabled('user_ldap');
  32. \OC_JSON::callCheck();
  33. $l = \OC::$server->getL10N('user_ldap');
  34. $ldapWrapper = new OCA\User_LDAP\LDAP();
  35. $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $_POST['ldap_serverconfig_chooser']);
  36. try {
  37. $configurationOk = true;
  38. $conf = $connection->getConfiguration();
  39. if ($conf['ldap_configuration_active'] === '0') {
  40. //needs to be true, otherwise it will also fail with an irritating message
  41. $conf['ldap_configuration_active'] = '1';
  42. $configurationOk = $connection->setConfiguration($conf);
  43. }
  44. if ($configurationOk) {
  45. //Configuration is okay
  46. /*
  47. * Clossing the session since it won't be used from this point on. There might be a potential
  48. * race condition if a second request is made: either this request or the other might not
  49. * contact the LDAP backup server the first time when it should, but there shouldn't be any
  50. * problem with that other than the extra connection.
  51. */
  52. \OC::$server->getSession()->close();
  53. if ($connection->bind()) {
  54. /*
  55. * This shiny if block is an ugly hack to find out whether anonymous
  56. * bind is possible on AD or not. Because AD happily and constantly
  57. * replies with success to any anonymous bind request, we need to
  58. * fire up a broken operation. If AD does not allow anonymous bind,
  59. * it will end up with LDAP error code 1 which is turned into an
  60. * exception by the LDAP wrapper. We catch this. Other cases may
  61. * pass (like e.g. expected syntax error).
  62. */
  63. try {
  64. $ldapWrapper->read($connection->getConnectionResource(), '', 'objectClass=*', array('dn'));
  65. } catch (\Exception $e) {
  66. if($e->getCode() === 1) {
  67. \OC_JSON::error(array('message' => $l->t('Invalid configuration: Anonymous binding is not allowed.')));
  68. exit;
  69. }
  70. }
  71. \OC_JSON::success(array('message'
  72. => $l->t('Valid configuration, connection established!')));
  73. } else {
  74. \OC_JSON::error(array('message'
  75. => $l->t('Valid configuration, but binding failed. Please check the server settings and credentials.')));
  76. }
  77. } else {
  78. \OC_JSON::error(array('message'
  79. => $l->t('Invalid configuration. Please have a look at the logs for further details.')));
  80. }
  81. } catch (\Exception $e) {
  82. \OC_JSON::error(array('message' => $e->getMessage()));
  83. }