Configuration.php 16 KB

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