Configuration.php 18 KB

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