Configuration.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Alex Weirig <alex.weirig@technolink.lu>
  6. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lennart Rosam <hello@takuto.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roger Szabo <roger.szabo@web.de>
  15. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  16. * @author Xuanwo <xuanwo@yunify.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\User_LDAP;
  34. /**
  35. * @property int ldapPagingSize holds an integer
  36. */
  37. class Configuration {
  38. protected $configPrefix = null;
  39. protected $configRead = false;
  40. /**
  41. * @var string[] pre-filled with one reference key so that at least one entry is written on save request and
  42. * the config ID is registered
  43. */
  44. protected $unsavedChanges = ['ldapConfigurationActive' => 'ldapConfigurationActive'];
  45. //settings
  46. protected $config = array(
  47. 'ldapHost' => null,
  48. 'ldapPort' => null,
  49. 'ldapBackupHost' => null,
  50. 'ldapBackupPort' => null,
  51. 'ldapBase' => null,
  52. 'ldapBaseUsers' => null,
  53. 'ldapBaseGroups' => null,
  54. 'ldapAgentName' => null,
  55. 'ldapAgentPassword' => null,
  56. 'ldapTLS' => null,
  57. 'turnOffCertCheck' => null,
  58. 'ldapIgnoreNamingRules' => null,
  59. 'ldapUserDisplayName' => null,
  60. 'ldapUserDisplayName2' => null,
  61. 'ldapGidNumber' => null,
  62. 'ldapUserFilterObjectclass' => null,
  63. 'ldapUserFilterGroups' => null,
  64. 'ldapUserFilter' => null,
  65. 'ldapUserFilterMode' => null,
  66. 'ldapGroupFilter' => null,
  67. 'ldapGroupFilterMode' => null,
  68. 'ldapGroupFilterObjectclass' => null,
  69. 'ldapGroupFilterGroups' => null,
  70. 'ldapGroupDisplayName' => null,
  71. 'ldapGroupMemberAssocAttr' => null,
  72. 'ldapLoginFilter' => null,
  73. 'ldapLoginFilterMode' => null,
  74. 'ldapLoginFilterEmail' => null,
  75. 'ldapLoginFilterUsername' => null,
  76. 'ldapLoginFilterAttributes' => null,
  77. 'ldapQuotaAttribute' => null,
  78. 'ldapQuotaDefault' => null,
  79. 'ldapEmailAttribute' => null,
  80. 'ldapCacheTTL' => null,
  81. 'ldapUuidUserAttribute' => 'auto',
  82. 'ldapUuidGroupAttribute' => 'auto',
  83. 'ldapOverrideMainServer' => false,
  84. 'ldapConfigurationActive' => false,
  85. 'ldapAttributesForUserSearch' => null,
  86. 'ldapAttributesForGroupSearch' => null,
  87. 'ldapExperiencedAdmin' => false,
  88. 'homeFolderNamingRule' => null,
  89. 'hasPagedResultSupport' => false,
  90. 'hasMemberOfFilterSupport' => false,
  91. 'useMemberOfToDetectMembership' => true,
  92. 'ldapExpertUsernameAttr' => null,
  93. 'ldapExpertUUIDUserAttr' => null,
  94. 'ldapExpertUUIDGroupAttr' => null,
  95. 'lastJpegPhotoLookup' => null,
  96. 'ldapNestedGroups' => false,
  97. 'ldapPagingSize' => null,
  98. 'turnOnPasswordChange' => false,
  99. 'ldapDynamicGroupMemberURL' => null,
  100. 'ldapDefaultPPolicyDN' => null,
  101. );
  102. /**
  103. * @param string $configPrefix
  104. * @param bool $autoRead
  105. */
  106. public function __construct($configPrefix, $autoRead = true) {
  107. $this->configPrefix = $configPrefix;
  108. if($autoRead) {
  109. $this->readConfiguration();
  110. }
  111. }
  112. /**
  113. * @param string $name
  114. * @return mixed|null
  115. */
  116. public function __get($name) {
  117. if(isset($this->config[$name])) {
  118. return $this->config[$name];
  119. }
  120. return null;
  121. }
  122. /**
  123. * @param string $name
  124. * @param mixed $value
  125. */
  126. public function __set($name, $value) {
  127. $this->setConfiguration(array($name => $value));
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function getConfiguration() {
  133. return $this->config;
  134. }
  135. /**
  136. * set LDAP configuration with values delivered by an array, not read
  137. * from configuration. It does not save the configuration! To do so, you
  138. * must call saveConfiguration afterwards.
  139. * @param array $config array that holds the config parameters in an associated
  140. * array
  141. * @param array &$applied optional; array where the set fields will be given to
  142. * @return false|null
  143. */
  144. public function setConfiguration($config, &$applied = null) {
  145. if(!is_array($config)) {
  146. return false;
  147. }
  148. $cta = $this->getConfigTranslationArray();
  149. foreach($config as $inputKey => $val) {
  150. if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
  151. $key = $cta[$inputKey];
  152. } elseif(array_key_exists($inputKey, $this->config)) {
  153. $key = $inputKey;
  154. } else {
  155. continue;
  156. }
  157. $setMethod = 'setValue';
  158. switch($key) {
  159. case 'ldapAgentPassword':
  160. $setMethod = 'setRawValue';
  161. break;
  162. case 'homeFolderNamingRule':
  163. $trimmedVal = trim($val);
  164. if ($trimmedVal !== '' && strpos($val, 'attr:') === false) {
  165. $val = 'attr:'.$trimmedVal;
  166. }
  167. break;
  168. case 'ldapBase':
  169. case 'ldapBaseUsers':
  170. case 'ldapBaseGroups':
  171. case 'ldapAttributesForUserSearch':
  172. case 'ldapAttributesForGroupSearch':
  173. case 'ldapUserFilterObjectclass':
  174. case 'ldapUserFilterGroups':
  175. case 'ldapGroupFilterObjectclass':
  176. case 'ldapGroupFilterGroups':
  177. case 'ldapLoginFilterAttributes':
  178. $setMethod = 'setMultiLine';
  179. break;
  180. }
  181. $this->$setMethod($key, $val);
  182. if(is_array($applied)) {
  183. $applied[] = $inputKey;
  184. // storing key as index avoids duplication, and as value for simplicity
  185. }
  186. $this->unsavedChanges[$key] = $key;
  187. }
  188. return null;
  189. }
  190. public function readConfiguration() {
  191. if(!$this->configRead && !is_null($this->configPrefix)) {
  192. $cta = array_flip($this->getConfigTranslationArray());
  193. foreach($this->config as $key => $val) {
  194. if(!isset($cta[$key])) {
  195. //some are determined
  196. continue;
  197. }
  198. $dbKey = $cta[$key];
  199. switch($key) {
  200. case 'ldapBase':
  201. case 'ldapBaseUsers':
  202. case 'ldapBaseGroups':
  203. case 'ldapAttributesForUserSearch':
  204. case 'ldapAttributesForGroupSearch':
  205. case 'ldapUserFilterObjectclass':
  206. case 'ldapUserFilterGroups':
  207. case 'ldapGroupFilterObjectclass':
  208. case 'ldapGroupFilterGroups':
  209. case 'ldapLoginFilterAttributes':
  210. $readMethod = 'getMultiLine';
  211. break;
  212. case 'ldapIgnoreNamingRules':
  213. $readMethod = 'getSystemValue';
  214. $dbKey = $key;
  215. break;
  216. case 'ldapAgentPassword':
  217. $readMethod = 'getPwd';
  218. break;
  219. case 'ldapUserDisplayName2':
  220. case 'ldapGroupDisplayName':
  221. $readMethod = 'getLcValue';
  222. break;
  223. case 'ldapUserDisplayName':
  224. default:
  225. // user display name does not lower case because
  226. // we rely on an upper case N as indicator whether to
  227. // auto-detect it or not. FIXME
  228. $readMethod = 'getValue';
  229. break;
  230. }
  231. $this->config[$key] = $this->$readMethod($dbKey);
  232. }
  233. $this->configRead = true;
  234. }
  235. }
  236. /**
  237. * saves the current config changes in the database
  238. */
  239. public function saveConfiguration() {
  240. $cta = array_flip($this->getConfigTranslationArray());
  241. foreach($this->unsavedChanges as $key) {
  242. $value = $this->config[$key];
  243. switch ($key) {
  244. case 'ldapAgentPassword':
  245. $value = base64_encode($value);
  246. break;
  247. case 'ldapBase':
  248. case 'ldapBaseUsers':
  249. case 'ldapBaseGroups':
  250. case 'ldapAttributesForUserSearch':
  251. case 'ldapAttributesForGroupSearch':
  252. case 'ldapUserFilterObjectclass':
  253. case 'ldapUserFilterGroups':
  254. case 'ldapGroupFilterObjectclass':
  255. case 'ldapGroupFilterGroups':
  256. case 'ldapLoginFilterAttributes':
  257. if(is_array($value)) {
  258. $value = implode("\n", $value);
  259. }
  260. break;
  261. //following options are not stored but detected, skip them
  262. case 'ldapIgnoreNamingRules':
  263. case 'hasPagedResultSupport':
  264. case 'ldapUuidUserAttribute':
  265. case 'ldapUuidGroupAttribute':
  266. continue 2;
  267. }
  268. if(is_null($value)) {
  269. $value = '';
  270. }
  271. $this->saveValue($cta[$key], $value);
  272. }
  273. $this->saveValue('_lastChange', time());
  274. $this->unsavedChanges = [];
  275. }
  276. /**
  277. * @param string $varName
  278. * @return array|string
  279. */
  280. protected function getMultiLine($varName) {
  281. $value = $this->getValue($varName);
  282. if(empty($value)) {
  283. $value = '';
  284. } else {
  285. $value = preg_split('/\r\n|\r|\n/', $value);
  286. }
  287. return $value;
  288. }
  289. /**
  290. * Sets multi-line values as arrays
  291. *
  292. * @param string $varName name of config-key
  293. * @param array|string $value to set
  294. */
  295. protected function setMultiLine($varName, $value) {
  296. if(empty($value)) {
  297. $value = '';
  298. } else if (!is_array($value)) {
  299. $value = preg_split('/\r\n|\r|\n|;/', $value);
  300. if($value === false) {
  301. $value = '';
  302. }
  303. }
  304. if(!is_array($value)) {
  305. $finalValue = trim($value);
  306. } else {
  307. $finalValue = [];
  308. foreach($value as $key => $val) {
  309. if(is_string($val)) {
  310. $val = trim($val);
  311. if ($val !== '') {
  312. //accidental line breaks are not wanted and can cause
  313. // odd behaviour. Thus, away with them.
  314. $finalValue[] = $val;
  315. }
  316. } else {
  317. $finalValue[] = $val;
  318. }
  319. }
  320. }
  321. $this->setRawValue($varName, $finalValue);
  322. }
  323. /**
  324. * @param string $varName
  325. * @return string
  326. */
  327. protected function getPwd($varName) {
  328. return base64_decode($this->getValue($varName));
  329. }
  330. /**
  331. * @param string $varName
  332. * @return string
  333. */
  334. protected function getLcValue($varName) {
  335. return mb_strtolower($this->getValue($varName), 'UTF-8');
  336. }
  337. /**
  338. * @param string $varName
  339. * @return string
  340. */
  341. protected function getSystemValue($varName) {
  342. //FIXME: if another system value is added, softcode the default value
  343. return \OC::$server->getConfig()->getSystemValue($varName, false);
  344. }
  345. /**
  346. * @param string $varName
  347. * @return string
  348. */
  349. protected function getValue($varName) {
  350. static $defaults;
  351. if(is_null($defaults)) {
  352. $defaults = $this->getDefaults();
  353. }
  354. return \OC::$server->getConfig()->getAppValue('user_ldap',
  355. $this->configPrefix.$varName,
  356. $defaults[$varName]);
  357. }
  358. /**
  359. * Sets a scalar value.
  360. *
  361. * @param string $varName name of config key
  362. * @param mixed $value to set
  363. */
  364. protected function setValue($varName, $value) {
  365. if(is_string($value)) {
  366. $value = trim($value);
  367. }
  368. $this->config[$varName] = $value;
  369. }
  370. /**
  371. * Sets a scalar value without trimming.
  372. *
  373. * @param string $varName name of config key
  374. * @param mixed $value to set
  375. */
  376. protected function setRawValue($varName, $value) {
  377. $this->config[$varName] = $value;
  378. }
  379. /**
  380. * @param string $varName
  381. * @param string $value
  382. * @return bool
  383. */
  384. protected function saveValue($varName, $value) {
  385. \OC::$server->getConfig()->setAppValue(
  386. 'user_ldap',
  387. $this->configPrefix.$varName,
  388. $value
  389. );
  390. return true;
  391. }
  392. /**
  393. * @return array an associative array with the default values. Keys are correspond
  394. * to config-value entries in the database table
  395. */
  396. public function getDefaults() {
  397. return array(
  398. 'ldap_host' => '',
  399. 'ldap_port' => '',
  400. 'ldap_backup_host' => '',
  401. 'ldap_backup_port' => '',
  402. 'ldap_override_main_server' => '',
  403. 'ldap_dn' => '',
  404. 'ldap_agent_password' => '',
  405. 'ldap_base' => '',
  406. 'ldap_base_users' => '',
  407. 'ldap_base_groups' => '',
  408. 'ldap_userlist_filter' => '',
  409. 'ldap_user_filter_mode' => 0,
  410. 'ldap_userfilter_objectclass' => '',
  411. 'ldap_userfilter_groups' => '',
  412. 'ldap_login_filter' => '',
  413. 'ldap_login_filter_mode' => 0,
  414. 'ldap_loginfilter_email' => 0,
  415. 'ldap_loginfilter_username' => 1,
  416. 'ldap_loginfilter_attributes' => '',
  417. 'ldap_group_filter' => '',
  418. 'ldap_group_filter_mode' => 0,
  419. 'ldap_groupfilter_objectclass' => '',
  420. 'ldap_groupfilter_groups' => '',
  421. 'ldap_gid_number' => 'gidNumber',
  422. 'ldap_display_name' => 'displayName',
  423. 'ldap_user_display_name_2' => '',
  424. 'ldap_group_display_name' => 'cn',
  425. 'ldap_tls' => 0,
  426. 'ldap_quota_def' => '',
  427. 'ldap_quota_attr' => '',
  428. 'ldap_email_attr' => '',
  429. 'ldap_group_member_assoc_attribute' => 'uniqueMember',
  430. 'ldap_cache_ttl' => 600,
  431. 'ldap_uuid_user_attribute' => 'auto',
  432. 'ldap_uuid_group_attribute' => 'auto',
  433. 'home_folder_naming_rule' => '',
  434. 'ldap_turn_off_cert_check' => 0,
  435. 'ldap_configuration_active' => 0,
  436. 'ldap_attributes_for_user_search' => '',
  437. 'ldap_attributes_for_group_search' => '',
  438. 'ldap_expert_username_attr' => '',
  439. 'ldap_expert_uuid_user_attr' => '',
  440. 'ldap_expert_uuid_group_attr' => '',
  441. 'has_memberof_filter_support' => 0,
  442. 'use_memberof_to_detect_membership' => 1,
  443. 'last_jpegPhoto_lookup' => 0,
  444. 'ldap_nested_groups' => 0,
  445. 'ldap_paging_size' => 500,
  446. 'ldap_turn_on_pwd_change' => 0,
  447. 'ldap_experienced_admin' => 0,
  448. 'ldap_dynamic_group_member_url' => '',
  449. 'ldap_default_ppolicy_dn' => '',
  450. );
  451. }
  452. /**
  453. * @return array that maps internal variable names to database fields
  454. */
  455. public function getConfigTranslationArray() {
  456. //TODO: merge them into one representation
  457. static $array = array(
  458. 'ldap_host' => 'ldapHost',
  459. 'ldap_port' => 'ldapPort',
  460. 'ldap_backup_host' => 'ldapBackupHost',
  461. 'ldap_backup_port' => 'ldapBackupPort',
  462. 'ldap_override_main_server' => 'ldapOverrideMainServer',
  463. 'ldap_dn' => 'ldapAgentName',
  464. 'ldap_agent_password' => 'ldapAgentPassword',
  465. 'ldap_base' => 'ldapBase',
  466. 'ldap_base_users' => 'ldapBaseUsers',
  467. 'ldap_base_groups' => 'ldapBaseGroups',
  468. 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
  469. 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
  470. 'ldap_userlist_filter' => 'ldapUserFilter',
  471. 'ldap_user_filter_mode' => 'ldapUserFilterMode',
  472. 'ldap_login_filter' => 'ldapLoginFilter',
  473. 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
  474. 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
  475. 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
  476. 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
  477. 'ldap_group_filter' => 'ldapGroupFilter',
  478. 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
  479. 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
  480. 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
  481. 'ldap_gid_number' => 'ldapGidNumber',
  482. 'ldap_display_name' => 'ldapUserDisplayName',
  483. 'ldap_user_display_name_2' => 'ldapUserDisplayName2',
  484. 'ldap_group_display_name' => 'ldapGroupDisplayName',
  485. 'ldap_tls' => 'ldapTLS',
  486. 'ldap_quota_def' => 'ldapQuotaDefault',
  487. 'ldap_quota_attr' => 'ldapQuotaAttribute',
  488. 'ldap_email_attr' => 'ldapEmailAttribute',
  489. 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
  490. 'ldap_cache_ttl' => 'ldapCacheTTL',
  491. 'home_folder_naming_rule' => 'homeFolderNamingRule',
  492. 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
  493. 'ldap_configuration_active' => 'ldapConfigurationActive',
  494. 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
  495. 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
  496. 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
  497. 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
  498. 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
  499. 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
  500. 'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
  501. 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
  502. 'ldap_nested_groups' => 'ldapNestedGroups',
  503. 'ldap_paging_size' => 'ldapPagingSize',
  504. 'ldap_turn_on_pwd_change' => 'turnOnPasswordChange',
  505. 'ldap_experienced_admin' => 'ldapExperiencedAdmin',
  506. 'ldap_dynamic_group_member_url' => 'ldapDynamicGroupMemberURL',
  507. 'ldap_default_ppolicy_dn' => 'ldapDefaultPPolicyDN',
  508. 'ldapIgnoreNamingRules' => 'ldapIgnoreNamingRules', // sysconfig
  509. );
  510. return $array;
  511. }
  512. }