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.

Configuration.php 16KB

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