1
0

Configuration.php 17 KB

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