You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
10 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\Controller;
  25. use OC\CapabilitiesManager;
  26. use OC\Core\Controller\OCSController;
  27. use OC\Security\IdentityProof\Manager;
  28. use OCA\User_LDAP\Configuration;
  29. use OCA\User_LDAP\ConnectionFactory;
  30. use OCA\User_LDAP\Helper;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCS\OCSBadRequestException;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\OCS\OCSNotFoundException;
  35. use OCP\ILogger;
  36. use OCP\IRequest;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. class ConfigAPIController extends OCSController {
  40. /** @var Helper */
  41. private $ldapHelper;
  42. /** @var ILogger */
  43. private $logger;
  44. /** @var ConnectionFactory */
  45. private $connectionFactory;
  46. public function __construct(
  47. $appName,
  48. IRequest $request,
  49. CapabilitiesManager $capabilitiesManager,
  50. IUserSession $userSession,
  51. IUserManager $userManager,
  52. Manager $keyManager,
  53. Helper $ldapHelper,
  54. ILogger $logger,
  55. ConnectionFactory $connectionFactory
  56. ) {
  57. parent::__construct(
  58. $appName,
  59. $request,
  60. $capabilitiesManager,
  61. $userSession,
  62. $userManager,
  63. $keyManager
  64. );
  65. $this->ldapHelper = $ldapHelper;
  66. $this->logger = $logger;
  67. $this->connectionFactory = $connectionFactory;
  68. }
  69. /**
  70. * creates a new (empty) configuration and returns the resulting prefix
  71. *
  72. * Example: curl -X POST -H "OCS-APIREQUEST: true" -u $admin:$password \
  73. * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config
  74. *
  75. * results in:
  76. *
  77. * <?xml version="1.0"?>
  78. * <ocs>
  79. * <meta>
  80. * <status>ok</status>
  81. * <statuscode>200</statuscode>
  82. * <message>OK</message>
  83. * </meta>
  84. * <data>
  85. * <configID>s40</configID>
  86. * </data>
  87. * </ocs>
  88. *
  89. * Failing example: if an exception is thrown (e.g. Database connection lost)
  90. * the detailed error will be logged. The output will then look like:
  91. *
  92. * <?xml version="1.0"?>
  93. * <ocs>
  94. * <meta>
  95. * <status>failure</status>
  96. * <statuscode>999</statuscode>
  97. * <message>An issue occurred when creating the new config.</message>
  98. * </meta>
  99. * <data/>
  100. * </ocs>
  101. *
  102. * For JSON output provide the format=json parameter
  103. *
  104. * @return DataResponse
  105. * @throws OCSException
  106. */
  107. public function create() {
  108. try {
  109. $configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix();
  110. $configHolder = new Configuration($configPrefix);
  111. $configHolder->saveConfiguration();
  112. } catch (\Exception $e) {
  113. $this->logger->logException($e);
  114. throw new OCSException('An issue occurred when creating the new config.');
  115. }
  116. return new DataResponse(['configID' => $configPrefix]);
  117. }
  118. /**
  119. * Deletes a LDAP configuration, if present.
  120. *
  121. * Example:
  122. * curl -X DELETE -H "OCS-APIREQUEST: true" -u $admin:$password \
  123. * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60
  124. *
  125. * <?xml version="1.0"?>
  126. * <ocs>
  127. * <meta>
  128. * <status>ok</status>
  129. * <statuscode>200</statuscode>
  130. * <message>OK</message>
  131. * </meta>
  132. * <data/>
  133. * </ocs>
  134. *
  135. * @param string $configID
  136. * @return DataResponse
  137. * @throws OCSBadRequestException
  138. * @throws OCSException
  139. */
  140. public function delete($configID) {
  141. try {
  142. $this->ensureConfigIDExists($configID);
  143. if (!$this->ldapHelper->deleteServerConfiguration($configID)) {
  144. throw new OCSException('Could not delete configuration');
  145. }
  146. } catch (OCSException $e) {
  147. throw $e;
  148. } catch (\Exception $e) {
  149. $this->logger->logException($e);
  150. throw new OCSException('An issue occurred when deleting the config.');
  151. }
  152. return new DataResponse();
  153. }
  154. /**
  155. * modifies a configuration
  156. *
  157. * Example:
  158. * curl -X PUT -d "configData[ldapHost]=ldaps://my.ldap.server&configData[ldapPort]=636" \
  159. * -H "OCS-APIREQUEST: true" -u $admin:$password \
  160. * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60
  161. *
  162. * <?xml version="1.0"?>
  163. * <ocs>
  164. * <meta>
  165. * <status>ok</status>
  166. * <statuscode>200</statuscode>
  167. * <message>OK</message>
  168. * </meta>
  169. * <data/>
  170. * </ocs>
  171. *
  172. * @param string $configID
  173. * @param array $configData
  174. * @return DataResponse
  175. * @throws OCSException
  176. */
  177. public function modify($configID, $configData) {
  178. try {
  179. $this->ensureConfigIDExists($configID);
  180. if (!is_array($configData)) {
  181. throw new OCSBadRequestException('configData is not properly set');
  182. }
  183. $configuration = new Configuration($configID);
  184. $configKeys = $configuration->getConfigTranslationArray();
  185. foreach ($configKeys as $i => $key) {
  186. if (isset($configData[$key])) {
  187. $configuration->$key = $configData[$key];
  188. }
  189. }
  190. $configuration->saveConfiguration();
  191. $this->connectionFactory->get($configID)->clearCache();
  192. } catch (OCSException $e) {
  193. throw $e;
  194. } catch (\Exception $e) {
  195. $this->logger->logException($e);
  196. throw new OCSException('An issue occurred when modifying the config.');
  197. }
  198. return new DataResponse();
  199. }
  200. /**
  201. * retrieves a configuration
  202. *
  203. * <?xml version="1.0"?>
  204. * <ocs>
  205. * <meta>
  206. * <status>ok</status>
  207. * <statuscode>200</statuscode>
  208. * <message>OK</message>
  209. * </meta>
  210. * <data>
  211. * <ldapHost>ldaps://my.ldap.server</ldapHost>
  212. * <ldapPort>7770</ldapPort>
  213. * <ldapBackupHost></ldapBackupHost>
  214. * <ldapBackupPort></ldapBackupPort>
  215. * <ldapBase>ou=small,dc=my,dc=ldap,dc=server</ldapBase>
  216. * <ldapBaseUsers>ou=users,ou=small,dc=my,dc=ldap,dc=server</ldapBaseUsers>
  217. * <ldapBaseGroups>ou=small,dc=my,dc=ldap,dc=server</ldapBaseGroups>
  218. * <ldapAgentName>cn=root,dc=my,dc=ldap,dc=server</ldapAgentName>
  219. * <ldapAgentPassword>clearTextWithShowPassword=1</ldapAgentPassword>
  220. * <ldapTLS>1</ldapTLS>
  221. * <turnOffCertCheck>0</turnOffCertCheck>
  222. * <ldapIgnoreNamingRules/>
  223. * <ldapUserDisplayName>displayname</ldapUserDisplayName>
  224. * <ldapUserDisplayName2>uid</ldapUserDisplayName2>
  225. * <ldapUserFilterObjectclass>inetOrgPerson</ldapUserFilterObjectclass>
  226. * <ldapUserFilterGroups></ldapUserFilterGroups>
  227. * <ldapUserFilter>(&amp;(objectclass=nextcloudUser)(nextcloudEnabled=TRUE))</ldapUserFilter>
  228. * <ldapUserFilterMode>1</ldapUserFilterMode>
  229. * <ldapGroupFilter>(&amp;(|(objectclass=nextcloudGroup)))</ldapGroupFilter>
  230. * <ldapGroupFilterMode>0</ldapGroupFilterMode>
  231. * <ldapGroupFilterObjectclass>nextcloudGroup</ldapGroupFilterObjectclass>
  232. * <ldapGroupFilterGroups></ldapGroupFilterGroups>
  233. * <ldapGroupDisplayName>cn</ldapGroupDisplayName>
  234. * <ldapGroupMemberAssocAttr>memberUid</ldapGroupMemberAssocAttr>
  235. * <ldapLoginFilter>(&amp;(|(objectclass=inetOrgPerson))(uid=%uid))</ldapLoginFilter>
  236. * <ldapLoginFilterMode>0</ldapLoginFilterMode>
  237. * <ldapLoginFilterEmail>0</ldapLoginFilterEmail>
  238. * <ldapLoginFilterUsername>1</ldapLoginFilterUsername>
  239. * <ldapLoginFilterAttributes></ldapLoginFilterAttributes>
  240. * <ldapQuotaAttribute></ldapQuotaAttribute>
  241. * <ldapQuotaDefault></ldapQuotaDefault>
  242. * <ldapEmailAttribute>mail</ldapEmailAttribute>
  243. * <ldapCacheTTL>20</ldapCacheTTL>
  244. * <ldapUuidUserAttribute>auto</ldapUuidUserAttribute>
  245. * <ldapUuidGroupAttribute>auto</ldapUuidGroupAttribute>
  246. * <ldapOverrideMainServer></ldapOverrideMainServer>
  247. * <ldapConfigurationActive>1</ldapConfigurationActive>
  248. * <ldapAttributesForUserSearch>uid;sn;givenname</ldapAttributesForUserSearch>
  249. * <ldapAttributesForGroupSearch></ldapAttributesForGroupSearch>
  250. * <ldapExperiencedAdmin>0</ldapExperiencedAdmin>
  251. * <homeFolderNamingRule></homeFolderNamingRule>
  252. * <hasMemberOfFilterSupport></hasMemberOfFilterSupport>
  253. * <useMemberOfToDetectMembership>1</useMemberOfToDetectMembership>
  254. * <ldapExpertUsernameAttr>uid</ldapExpertUsernameAttr>
  255. * <ldapExpertUUIDUserAttr>uid</ldapExpertUUIDUserAttr>
  256. * <ldapExpertUUIDGroupAttr></ldapExpertUUIDGroupAttr>
  257. * <lastJpegPhotoLookup>0</lastJpegPhotoLookup>
  258. * <ldapNestedGroups>0</ldapNestedGroups>
  259. * <ldapPagingSize>500</ldapPagingSize>
  260. * <turnOnPasswordChange>1</turnOnPasswordChange>
  261. * <ldapDynamicGroupMemberURL></ldapDynamicGroupMemberURL>
  262. * </data>
  263. * </ocs>
  264. *
  265. * @param string $configID
  266. * @param bool|string $showPassword
  267. * @return DataResponse
  268. * @throws OCSException
  269. */
  270. public function show($configID, $showPassword = false) {
  271. try {
  272. $this->ensureConfigIDExists($configID);
  273. $config = new Configuration($configID);
  274. $data = $config->getConfiguration();
  275. if (!(int)$showPassword) {
  276. $data['ldapAgentPassword'] = '***';
  277. }
  278. foreach ($data as $key => $value) {
  279. if (is_array($value)) {
  280. $value = implode(';', $value);
  281. $data[$key] = $value;
  282. }
  283. }
  284. } catch (OCSException $e) {
  285. throw $e;
  286. } catch (\Exception $e) {
  287. $this->logger->logException($e);
  288. throw new OCSException('An issue occurred when modifying the config.');
  289. }
  290. return new DataResponse($data);
  291. }
  292. /**
  293. * if the given config ID is not available, an exception is thrown
  294. *
  295. * @param string $configID
  296. * @throws OCSNotFoundException
  297. */
  298. private function ensureConfigIDExists($configID) {
  299. $prefixes = $this->ldapHelper->getServerConfigurationPrefixes();
  300. if (!in_array($configID, $prefixes, true)) {
  301. throw new OCSNotFoundException('Config ID not found');
  302. }
  303. }
  304. }