Access.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Aaron Wood <aaronjwood@gmail.com>
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Bart Visscher <bartv@thisnet.nl>
  9. * @author Benjamin Diele <benjamin@diele.be>
  10. * @author bline <scottbeck@gmail.com>
  11. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  12. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  13. * @author J0WI <J0WI@users.noreply.github.com>
  14. * @author Joas Schilling <coding@schilljs.com>
  15. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  16. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  17. * @author Lorenzo M. Catucci <lorenzo@sancho.ccd.uniroma2.it>
  18. * @author Lukas Reschke <lukas@statuscode.ch>
  19. * @author Mario Kolling <mario.kolling@serpro.gov.br>
  20. * @author Max Kovalenko <mxss1998@yandex.ru>
  21. * @author Morris Jobke <hey@morrisjobke.de>
  22. * @author Nicolas Grekas <nicolas.grekas@gmail.com>
  23. * @author Peter Kubica <peter@kubica.ch>
  24. * @author Ralph Krimmel <rkrimme1@gwdg.de>
  25. * @author Robin McCorkell <robin@mccorkell.me.uk>
  26. * @author Roeland Jago Douma <roeland@famdouma.nl>
  27. * @author Roger Szabo <roger.szabo@web.de>
  28. * @author Roland Tapken <roland@bitarbeiter.net>
  29. * @author root <root@localhost.localdomain>
  30. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  31. *
  32. * @license AGPL-3.0
  33. *
  34. * This code is free software: you can redistribute it and/or modify
  35. * it under the terms of the GNU Affero General Public License, version 3,
  36. * as published by the Free Software Foundation.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU Affero General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU Affero General Public License, version 3,
  44. * along with this program. If not, see <http://www.gnu.org/licenses/>
  45. *
  46. */
  47. namespace OCA\User_LDAP;
  48. use DomainException;
  49. use OC\Hooks\PublicEmitter;
  50. use OC\ServerNotAvailableException;
  51. use OCA\User_LDAP\Exceptions\ConstraintViolationException;
  52. use OCA\User_LDAP\Exceptions\NoMoreResults;
  53. use OCA\User_LDAP\Mapping\AbstractMapping;
  54. use OCA\User_LDAP\User\Manager;
  55. use OCA\User_LDAP\User\OfflineUser;
  56. use OCP\HintException;
  57. use OCP\IConfig;
  58. use OCP\IUserManager;
  59. use Psr\Log\LoggerInterface;
  60. use function strlen;
  61. use function substr;
  62. /**
  63. * Class Access
  64. *
  65. * @package OCA\User_LDAP
  66. */
  67. class Access extends LDAPUtility {
  68. public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];
  69. /** @var \OCA\User_LDAP\Connection */
  70. public $connection;
  71. /** @var Manager */
  72. public $userManager;
  73. /**
  74. * never ever check this var directly, always use getPagedSearchResultState
  75. * @var ?bool
  76. */
  77. protected $pagedSearchedSuccessful;
  78. /** @var ?AbstractMapping */
  79. protected $userMapper;
  80. /** @var ?AbstractMapping */
  81. protected $groupMapper;
  82. /**
  83. * @var \OCA\User_LDAP\Helper
  84. */
  85. private $helper;
  86. /** @var IConfig */
  87. private $config;
  88. /** @var IUserManager */
  89. private $ncUserManager;
  90. /** @var LoggerInterface */
  91. private $logger;
  92. private string $lastCookie = '';
  93. public function __construct(
  94. Connection $connection,
  95. ILDAPWrapper $ldap,
  96. Manager $userManager,
  97. Helper $helper,
  98. IConfig $config,
  99. IUserManager $ncUserManager,
  100. LoggerInterface $logger
  101. ) {
  102. parent::__construct($ldap);
  103. $this->connection = $connection;
  104. $this->userManager = $userManager;
  105. $this->userManager->setLdapAccess($this);
  106. $this->helper = $helper;
  107. $this->config = $config;
  108. $this->ncUserManager = $ncUserManager;
  109. $this->logger = $logger;
  110. }
  111. /**
  112. * sets the User Mapper
  113. */
  114. public function setUserMapper(AbstractMapping $mapper): void {
  115. $this->userMapper = $mapper;
  116. }
  117. /**
  118. * @throws \Exception
  119. */
  120. public function getUserMapper(): AbstractMapping {
  121. if (is_null($this->userMapper)) {
  122. throw new \Exception('UserMapper was not assigned to this Access instance.');
  123. }
  124. return $this->userMapper;
  125. }
  126. /**
  127. * sets the Group Mapper
  128. */
  129. public function setGroupMapper(AbstractMapping $mapper): void {
  130. $this->groupMapper = $mapper;
  131. }
  132. /**
  133. * returns the Group Mapper
  134. *
  135. * @throws \Exception
  136. */
  137. public function getGroupMapper(): AbstractMapping {
  138. if (is_null($this->groupMapper)) {
  139. throw new \Exception('GroupMapper was not assigned to this Access instance.');
  140. }
  141. return $this->groupMapper;
  142. }
  143. /**
  144. * @return bool
  145. */
  146. private function checkConnection() {
  147. return ($this->connection instanceof Connection);
  148. }
  149. /**
  150. * returns the Connection instance
  151. *
  152. * @return \OCA\User_LDAP\Connection
  153. */
  154. public function getConnection() {
  155. return $this->connection;
  156. }
  157. /**
  158. * reads a given attribute for an LDAP record identified by a DN
  159. *
  160. * @param string $dn the record in question
  161. * @param string $attr the attribute that shall be retrieved
  162. * if empty, just check the record's existence
  163. * @param string $filter
  164. * @return array|false an array of values on success or an empty
  165. * array if $attr is empty, false otherwise
  166. * @throws ServerNotAvailableException
  167. */
  168. public function readAttribute(string $dn, string $attr, string $filter = 'objectClass=*') {
  169. if (!$this->checkConnection()) {
  170. $this->logger->warning(
  171. 'No LDAP Connector assigned, access impossible for readAttribute.',
  172. ['app' => 'user_ldap']
  173. );
  174. return false;
  175. }
  176. $cr = $this->connection->getConnectionResource();
  177. if (!$this->ldap->isResource($cr)) {
  178. //LDAP not available
  179. $this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
  180. return false;
  181. }
  182. $attr = mb_strtolower($attr, 'UTF-8');
  183. // the actual read attribute later may contain parameters on a ranged
  184. // request, e.g. member;range=99-199. Depends on server reply.
  185. $attrToRead = $attr;
  186. $values = [];
  187. $isRangeRequest = false;
  188. do {
  189. $result = $this->executeRead($dn, $attrToRead, $filter);
  190. if (is_bool($result)) {
  191. // when an exists request was run and it was successful, an empty
  192. // array must be returned
  193. return $result ? [] : false;
  194. }
  195. if (!$isRangeRequest) {
  196. $values = $this->extractAttributeValuesFromResult($result, $attr);
  197. if (!empty($values)) {
  198. return $values;
  199. }
  200. }
  201. $isRangeRequest = false;
  202. $result = $this->extractRangeData($result, $attr);
  203. if (!empty($result)) {
  204. $normalizedResult = $this->extractAttributeValuesFromResult(
  205. [$attr => $result['values']],
  206. $attr
  207. );
  208. $values = array_merge($values, $normalizedResult);
  209. if ($result['rangeHigh'] === '*') {
  210. // when server replies with * as high range value, there are
  211. // no more results left
  212. return $values;
  213. } else {
  214. $low = $result['rangeHigh'] + 1;
  215. $attrToRead = $result['attributeName'] . ';range=' . $low . '-*';
  216. $isRangeRequest = true;
  217. }
  218. }
  219. } while ($isRangeRequest);
  220. $this->logger->debug('Requested attribute ' . $attr . ' not found for ' . $dn, ['app' => 'user_ldap']);
  221. return false;
  222. }
  223. /**
  224. * Runs an read operation against LDAP
  225. *
  226. * @return array|bool false if there was any error, true if an exists check
  227. * was performed and the requested DN found, array with the
  228. * returned data on a successful usual operation
  229. * @throws ServerNotAvailableException
  230. */
  231. public function executeRead(string $dn, string $attribute, string $filter) {
  232. $dn = $this->helper->DNasBaseParameter($dn);
  233. $rr = @$this->invokeLDAPMethod('read', $dn, $filter, [$attribute]);
  234. if (!$this->ldap->isResource($rr)) {
  235. if ($attribute !== '') {
  236. //do not throw this message on userExists check, irritates
  237. $this->logger->debug('readAttribute failed for DN ' . $dn, ['app' => 'user_ldap']);
  238. }
  239. //in case an error occurs , e.g. object does not exist
  240. return false;
  241. }
  242. if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $rr) === 1)) {
  243. $this->logger->debug('readAttribute: ' . $dn . ' found', ['app' => 'user_ldap']);
  244. return true;
  245. }
  246. $er = $this->invokeLDAPMethod('firstEntry', $rr);
  247. if (!$this->ldap->isResource($er)) {
  248. //did not match the filter, return false
  249. return false;
  250. }
  251. //LDAP attributes are not case sensitive
  252. $result = \OCP\Util::mb_array_change_key_case(
  253. $this->invokeLDAPMethod('getAttributes', $er), MB_CASE_LOWER, 'UTF-8');
  254. return $result;
  255. }
  256. /**
  257. * Normalizes a result grom getAttributes(), i.e. handles DNs and binary
  258. * data if present.
  259. *
  260. * @param array $result from ILDAPWrapper::getAttributes()
  261. * @param string $attribute the attribute name that was read
  262. * @return string[]
  263. */
  264. public function extractAttributeValuesFromResult($result, $attribute) {
  265. $values = [];
  266. if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) {
  267. $lowercaseAttribute = strtolower($attribute);
  268. for ($i = 0; $i < $result[$attribute]['count']; $i++) {
  269. if ($this->resemblesDN($attribute)) {
  270. $values[] = $this->helper->sanitizeDN($result[$attribute][$i]);
  271. } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') {
  272. $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);
  273. } else {
  274. $values[] = $result[$attribute][$i];
  275. }
  276. }
  277. }
  278. return $values;
  279. }
  280. /**
  281. * Attempts to find ranged data in a getAttribute results and extracts the
  282. * returned values as well as information on the range and full attribute
  283. * name for further processing.
  284. *
  285. * @param array $result from ILDAPWrapper::getAttributes()
  286. * @param string $attribute the attribute name that was read. Without ";range=…"
  287. * @return array If a range was detected with keys 'values', 'attributeName',
  288. * 'attributeFull' and 'rangeHigh', otherwise empty.
  289. */
  290. public function extractRangeData($result, $attribute) {
  291. $keys = array_keys($result);
  292. foreach ($keys as $key) {
  293. if ($key !== $attribute && str_starts_with((string)$key, $attribute)) {
  294. $queryData = explode(';', (string)$key);
  295. if (str_starts_with($queryData[1], 'range=')) {
  296. $high = substr($queryData[1], 1 + strpos($queryData[1], '-'));
  297. $data = [
  298. 'values' => $result[$key],
  299. 'attributeName' => $queryData[0],
  300. 'attributeFull' => $key,
  301. 'rangeHigh' => $high,
  302. ];
  303. return $data;
  304. }
  305. }
  306. }
  307. return [];
  308. }
  309. /**
  310. * Set password for an LDAP user identified by a DN
  311. *
  312. * @param string $userDN the user in question
  313. * @param string $password the new password
  314. * @return bool
  315. * @throws HintException
  316. * @throws \Exception
  317. */
  318. public function setPassword($userDN, $password) {
  319. if ((int)$this->connection->turnOnPasswordChange !== 1) {
  320. throw new \Exception('LDAP password changes are disabled.');
  321. }
  322. $cr = $this->connection->getConnectionResource();
  323. if (!$this->ldap->isResource($cr)) {
  324. //LDAP not available
  325. $this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
  326. return false;
  327. }
  328. try {
  329. // try PASSWD extended operation first
  330. return @$this->invokeLDAPMethod('exopPasswd', $userDN, '', $password) ||
  331. @$this->invokeLDAPMethod('modReplace', $userDN, $password);
  332. } catch (ConstraintViolationException $e) {
  333. throw new HintException('Password change rejected.', \OCP\Util::getL10N('user_ldap')->t('Password change rejected. Hint: ') . $e->getMessage(), (int)$e->getCode());
  334. }
  335. }
  336. /**
  337. * checks whether the given attributes value is probably a DN
  338. *
  339. * @param string $attr the attribute in question
  340. * @return boolean if so true, otherwise false
  341. */
  342. private function resemblesDN($attr) {
  343. $resemblingAttributes = [
  344. 'dn',
  345. 'uniquemember',
  346. 'member',
  347. // memberOf is an "operational" attribute, without a definition in any RFC
  348. 'memberof'
  349. ];
  350. return in_array($attr, $resemblingAttributes);
  351. }
  352. /**
  353. * checks whether the given string is probably a DN
  354. *
  355. * @param string $string
  356. * @return boolean
  357. */
  358. public function stringResemblesDN($string) {
  359. $r = $this->ldap->explodeDN($string, 0);
  360. // if exploding a DN succeeds and does not end up in
  361. // an empty array except for $r[count] being 0.
  362. return (is_array($r) && count($r) > 1);
  363. }
  364. /**
  365. * returns a DN-string that is cleaned from not domain parts, e.g.
  366. * cn=foo,cn=bar,dc=foobar,dc=server,dc=org
  367. * becomes dc=foobar,dc=server,dc=org
  368. *
  369. * @param string $dn
  370. * @return string
  371. */
  372. public function getDomainDNFromDN($dn) {
  373. $allParts = $this->ldap->explodeDN($dn, 0);
  374. if ($allParts === false) {
  375. //not a valid DN
  376. return '';
  377. }
  378. $domainParts = [];
  379. $dcFound = false;
  380. foreach ($allParts as $part) {
  381. if (!$dcFound && str_starts_with($part, 'dc=')) {
  382. $dcFound = true;
  383. }
  384. if ($dcFound) {
  385. $domainParts[] = $part;
  386. }
  387. }
  388. return implode(',', $domainParts);
  389. }
  390. /**
  391. * returns the LDAP DN for the given internal Nextcloud name of the group
  392. *
  393. * @param string $name the Nextcloud name in question
  394. * @return string|false LDAP DN on success, otherwise false
  395. */
  396. public function groupname2dn($name) {
  397. return $this->getGroupMapper()->getDNByName($name);
  398. }
  399. /**
  400. * returns the LDAP DN for the given internal Nextcloud name of the user
  401. *
  402. * @param string $name the Nextcloud name in question
  403. * @return string|false with the LDAP DN on success, otherwise false
  404. */
  405. public function username2dn($name) {
  406. $fdn = $this->getUserMapper()->getDNByName($name);
  407. //Check whether the DN belongs to the Base, to avoid issues on multi-
  408. //server setups
  409. if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
  410. return $fdn;
  411. }
  412. return false;
  413. }
  414. /**
  415. * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure
  416. *
  417. * @param string $fdn the dn of the group object
  418. * @param string $ldapName optional, the display name of the object
  419. * @return string|false with the name to use in Nextcloud, false on DN outside of search DN
  420. * @throws \Exception
  421. */
  422. public function dn2groupname($fdn, $ldapName = null) {
  423. //To avoid bypassing the base DN settings under certain circumstances
  424. //with the group support, check whether the provided DN matches one of
  425. //the given Bases
  426. if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
  427. return false;
  428. }
  429. return $this->dn2ocname($fdn, $ldapName, false);
  430. }
  431. /**
  432. * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure
  433. *
  434. * @param string $fdn the dn of the user object
  435. * @param string $ldapName optional, the display name of the object
  436. * @return string|false with with the name to use in Nextcloud
  437. * @throws \Exception
  438. */
  439. public function dn2username($fdn, $ldapName = null) {
  440. //To avoid bypassing the base DN settings under certain circumstances
  441. //with the group support, check whether the provided DN matches one of
  442. //the given Bases
  443. if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
  444. return false;
  445. }
  446. return $this->dn2ocname($fdn, $ldapName, true);
  447. }
  448. /**
  449. * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN
  450. *
  451. * @param string $fdn the dn of the user object
  452. * @param string|null $ldapName optional, the display name of the object
  453. * @param bool $isUser optional, whether it is a user object (otherwise group assumed)
  454. * @param bool|null $newlyMapped
  455. * @param array|null $record
  456. * @return false|string with with the name to use in Nextcloud
  457. * @throws \Exception
  458. */
  459. public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) {
  460. static $intermediates = [];
  461. if (isset($intermediates[($isUser ? 'user-' : 'group-') . $fdn])) {
  462. return false; // is a known intermediate
  463. }
  464. $newlyMapped = false;
  465. if ($isUser) {
  466. $mapper = $this->getUserMapper();
  467. $nameAttribute = $this->connection->ldapUserDisplayName;
  468. $filter = $this->connection->ldapUserFilter;
  469. } else {
  470. $mapper = $this->getGroupMapper();
  471. $nameAttribute = $this->connection->ldapGroupDisplayName;
  472. $filter = $this->connection->ldapGroupFilter;
  473. }
  474. //let's try to retrieve the Nextcloud name from the mappings table
  475. $ncName = $mapper->getNameByDN($fdn);
  476. if (is_string($ncName)) {
  477. return $ncName;
  478. }
  479. //second try: get the UUID and check if it is known. Then, update the DN and return the name.
  480. $uuid = $this->getUUID($fdn, $isUser, $record);
  481. if (is_string($uuid)) {
  482. $ncName = $mapper->getNameByUUID($uuid);
  483. if (is_string($ncName)) {
  484. $mapper->setDNbyUUID($fdn, $uuid);
  485. return $ncName;
  486. }
  487. } else {
  488. //If the UUID can't be detected something is foul.
  489. $this->logger->debug('Cannot determine UUID for ' . $fdn . '. Skipping.', ['app' => 'user_ldap']);
  490. return false;
  491. }
  492. if (is_null($ldapName)) {
  493. $ldapName = $this->readAttribute($fdn, $nameAttribute, $filter);
  494. if (!isset($ldapName[0]) || empty($ldapName[0])) {
  495. $this->logger->debug('No or empty name for ' . $fdn . ' with filter ' . $filter . '.', ['app' => 'user_ldap']);
  496. $intermediates[($isUser ? 'user-' : 'group-') . $fdn] = true;
  497. return false;
  498. }
  499. $ldapName = $ldapName[0];
  500. }
  501. if ($isUser) {
  502. $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
  503. if ($usernameAttribute !== '') {
  504. $username = $this->readAttribute($fdn, $usernameAttribute);
  505. if (!isset($username[0]) || empty($username[0])) {
  506. $this->logger->debug('No or empty username (' . $usernameAttribute . ') for ' . $fdn . '.', ['app' => 'user_ldap']);
  507. return false;
  508. }
  509. $username = $username[0];
  510. } else {
  511. $username = $uuid;
  512. }
  513. try {
  514. $intName = $this->sanitizeUsername($username);
  515. } catch (\InvalidArgumentException $e) {
  516. $this->logger->warning('Error sanitizing username: ' . $e->getMessage(), [
  517. 'exception' => $e,
  518. ]);
  519. // we don't attempt to set a username here. We can go for
  520. // for an alternative 4 digit random number as we would append
  521. // otherwise, however it's likely not enough space in bigger
  522. // setups, and most importantly: this is not intended.
  523. return false;
  524. }
  525. } else {
  526. $intName = $this->sanitizeGroupIDCandidate($ldapName);
  527. }
  528. //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups
  529. //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check
  530. //NOTE: mind, disabling cache affects only this instance! Using it
  531. // outside of core user management will still cache the user as non-existing.
  532. $originalTTL = $this->connection->ldapCacheTTL;
  533. $this->connection->setConfiguration(['ldapCacheTTL' => 0]);
  534. if ($intName !== ''
  535. && (($isUser && !$this->ncUserManager->userExists($intName))
  536. || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))
  537. )
  538. ) {
  539. $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
  540. $newlyMapped = $this->mapAndAnnounceIfApplicable($mapper, $fdn, $intName, $uuid, $isUser);
  541. if ($newlyMapped) {
  542. return $intName;
  543. }
  544. }
  545. $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
  546. $altName = $this->createAltInternalOwnCloudName($intName, $isUser);
  547. if (is_string($altName)) {
  548. if ($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) {
  549. $this->logger->warning(
  550. 'Mapped {fdn} as {altName} because of a name collision on {intName}.',
  551. [
  552. 'fdn' => $fdn,
  553. 'altName' => $altName,
  554. 'intName' => $intName,
  555. 'app' => 'user_ldap',
  556. ]
  557. );
  558. $newlyMapped = true;
  559. return $altName;
  560. }
  561. }
  562. //if everything else did not help..
  563. $this->logger->info('Could not create unique name for ' . $fdn . '.', ['app' => 'user_ldap']);
  564. return false;
  565. }
  566. public function mapAndAnnounceIfApplicable(
  567. AbstractMapping $mapper,
  568. string $fdn,
  569. string $name,
  570. string $uuid,
  571. bool $isUser
  572. ): bool {
  573. if ($mapper->map($fdn, $name, $uuid)) {
  574. if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
  575. $this->cacheUserExists($name);
  576. $this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
  577. } elseif (!$isUser) {
  578. $this->cacheGroupExists($name);
  579. }
  580. return true;
  581. }
  582. return false;
  583. }
  584. /**
  585. * gives back the user names as they are used ownClod internally
  586. *
  587. * @param array $ldapUsers as returned by fetchList()
  588. * @return array<int,string> an array with the user names to use in Nextcloud
  589. *
  590. * gives back the user names as they are used ownClod internally
  591. * @throws \Exception
  592. */
  593. public function nextcloudUserNames($ldapUsers) {
  594. return $this->ldap2NextcloudNames($ldapUsers, true);
  595. }
  596. /**
  597. * gives back the group names as they are used ownClod internally
  598. *
  599. * @param array $ldapGroups as returned by fetchList()
  600. * @return array<int,string> an array with the group names to use in Nextcloud
  601. *
  602. * gives back the group names as they are used ownClod internally
  603. * @throws \Exception
  604. */
  605. public function nextcloudGroupNames($ldapGroups) {
  606. return $this->ldap2NextcloudNames($ldapGroups, false);
  607. }
  608. /**
  609. * @param array[] $ldapObjects as returned by fetchList()
  610. * @return array<int,string>
  611. * @throws \Exception
  612. */
  613. private function ldap2NextcloudNames(array $ldapObjects, bool $isUsers): array {
  614. if ($isUsers) {
  615. $nameAttribute = $this->connection->ldapUserDisplayName;
  616. $sndAttribute = $this->connection->ldapUserDisplayName2;
  617. } else {
  618. $nameAttribute = $this->connection->ldapGroupDisplayName;
  619. $sndAttribute = null;
  620. }
  621. $nextcloudNames = [];
  622. foreach ($ldapObjects as $ldapObject) {
  623. $nameByLDAP = $ldapObject[$nameAttribute][0] ?? null;
  624. $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
  625. if ($ncName) {
  626. $nextcloudNames[] = $ncName;
  627. if ($isUsers) {
  628. $this->updateUserState($ncName);
  629. //cache the user names so it does not need to be retrieved
  630. //again later (e.g. sharing dialogue).
  631. if (is_null($nameByLDAP)) {
  632. continue;
  633. }
  634. $sndName = $ldapObject[$sndAttribute][0] ?? '';
  635. $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName);
  636. } elseif ($nameByLDAP !== null) {
  637. $this->cacheGroupDisplayName($ncName, $nameByLDAP);
  638. }
  639. }
  640. }
  641. return $nextcloudNames;
  642. }
  643. /**
  644. * removes the deleted-flag of a user if it was set
  645. *
  646. * @param string $ncname
  647. * @throws \Exception
  648. */
  649. public function updateUserState($ncname): void {
  650. $user = $this->userManager->get($ncname);
  651. if ($user instanceof OfflineUser) {
  652. $user->unmark();
  653. }
  654. }
  655. /**
  656. * caches the user display name
  657. *
  658. * @param string $ocName the internal Nextcloud username
  659. * @param string|false $home the home directory path
  660. */
  661. public function cacheUserHome(string $ocName, $home): void {
  662. $cacheKey = 'getHome' . $ocName;
  663. $this->connection->writeToCache($cacheKey, $home);
  664. }
  665. /**
  666. * caches a user as existing
  667. */
  668. public function cacheUserExists(string $ocName): void {
  669. $this->connection->writeToCache('userExists' . $ocName, true);
  670. }
  671. /**
  672. * caches a group as existing
  673. */
  674. public function cacheGroupExists(string $gid): void {
  675. $this->connection->writeToCache('groupExists' . $gid, true);
  676. }
  677. /**
  678. * caches the user display name
  679. *
  680. * @param string $ocName the internal Nextcloud username
  681. * @param string $displayName the display name
  682. * @param string $displayName2 the second display name
  683. * @throws \Exception
  684. */
  685. public function cacheUserDisplayName(string $ocName, string $displayName, string $displayName2 = ''): void {
  686. $user = $this->userManager->get($ocName);
  687. if ($user === null) {
  688. return;
  689. }
  690. $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
  691. $cacheKeyTrunk = 'getDisplayName';
  692. $this->connection->writeToCache($cacheKeyTrunk . $ocName, $displayName);
  693. }
  694. public function cacheGroupDisplayName(string $ncName, string $displayName): void {
  695. $cacheKey = 'group_getDisplayName' . $ncName;
  696. $this->connection->writeToCache($cacheKey, $displayName);
  697. }
  698. /**
  699. * creates a unique name for internal Nextcloud use for users. Don't call it directly.
  700. *
  701. * @param string $name the display name of the object
  702. * @return string|false with with the name to use in Nextcloud or false if unsuccessful
  703. *
  704. * Instead of using this method directly, call
  705. * createAltInternalOwnCloudName($name, true)
  706. */
  707. private function _createAltInternalOwnCloudNameForUsers(string $name) {
  708. $attempts = 0;
  709. //while loop is just a precaution. If a name is not generated within
  710. //20 attempts, something else is very wrong. Avoids infinite loop.
  711. while ($attempts < 20) {
  712. $altName = $name . '_' . rand(1000, 9999);
  713. if (!$this->ncUserManager->userExists($altName)) {
  714. return $altName;
  715. }
  716. $attempts++;
  717. }
  718. return false;
  719. }
  720. /**
  721. * creates a unique name for internal Nextcloud use for groups. Don't call it directly.
  722. *
  723. * @param string $name the display name of the object
  724. * @return string|false with with the name to use in Nextcloud or false if unsuccessful.
  725. *
  726. * Instead of using this method directly, call
  727. * createAltInternalOwnCloudName($name, false)
  728. *
  729. * Group names are also used as display names, so we do a sequential
  730. * numbering, e.g. Developers_42 when there are 41 other groups called
  731. * "Developers"
  732. */
  733. private function _createAltInternalOwnCloudNameForGroups(string $name) {
  734. $usedNames = $this->getGroupMapper()->getNamesBySearch($name, "", '_%');
  735. if (count($usedNames) === 0) {
  736. $lastNo = 1; //will become name_2
  737. } else {
  738. natsort($usedNames);
  739. $lastName = array_pop($usedNames);
  740. $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1);
  741. }
  742. $altName = $name . '_' . (string)($lastNo + 1);
  743. unset($usedNames);
  744. $attempts = 1;
  745. while ($attempts < 21) {
  746. // Check to be really sure it is unique
  747. // while loop is just a precaution. If a name is not generated within
  748. // 20 attempts, something else is very wrong. Avoids infinite loop.
  749. if (!\OC::$server->getGroupManager()->groupExists($altName)) {
  750. return $altName;
  751. }
  752. $altName = $name . '_' . ($lastNo + $attempts);
  753. $attempts++;
  754. }
  755. return false;
  756. }
  757. /**
  758. * creates a unique name for internal Nextcloud use.
  759. *
  760. * @param string $name the display name of the object
  761. * @param bool $isUser whether name should be created for a user (true) or a group (false)
  762. * @return string|false with with the name to use in Nextcloud or false if unsuccessful
  763. */
  764. private function createAltInternalOwnCloudName(string $name, bool $isUser) {
  765. // ensure there is space for the "_1234" suffix
  766. if (strlen($name) > 59) {
  767. $name = substr($name, 0, 59);
  768. }
  769. $originalTTL = $this->connection->ldapCacheTTL;
  770. $this->connection->setConfiguration(['ldapCacheTTL' => 0]);
  771. if ($isUser) {
  772. $altName = $this->_createAltInternalOwnCloudNameForUsers($name);
  773. } else {
  774. $altName = $this->_createAltInternalOwnCloudNameForGroups($name);
  775. }
  776. $this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
  777. return $altName;
  778. }
  779. /**
  780. * fetches a list of users according to a provided loginName and utilizing
  781. * the login filter.
  782. */
  783. public function fetchUsersByLoginName(string $loginName, array $attributes = ['dn']): array {
  784. $loginName = $this->escapeFilterPart($loginName);
  785. $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
  786. return $this->fetchListOfUsers($filter, $attributes);
  787. }
  788. /**
  789. * counts the number of users according to a provided loginName and
  790. * utilizing the login filter.
  791. *
  792. * @param string $loginName
  793. * @return false|int
  794. */
  795. public function countUsersByLoginName($loginName) {
  796. $loginName = $this->escapeFilterPart($loginName);
  797. $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter);
  798. return $this->countUsers($filter);
  799. }
  800. /**
  801. * @throws \Exception
  802. */
  803. public function fetchListOfUsers(string $filter, array $attr, int $limit = null, int $offset = null, bool $forceApplyAttributes = false): array {
  804. $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
  805. $recordsToUpdate = $ldapRecords;
  806. if (!$forceApplyAttributes) {
  807. $isBackgroundJobModeAjax = $this->config
  808. ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
  809. $listOfDNs = array_reduce($ldapRecords, function ($listOfDNs, $entry) {
  810. $listOfDNs[] = $entry['dn'][0];
  811. return $listOfDNs;
  812. }, []);
  813. $idsByDn = $this->getUserMapper()->getListOfIdsByDn($listOfDNs);
  814. $recordsToUpdate = array_filter($ldapRecords, function ($record) use ($isBackgroundJobModeAjax, $idsByDn) {
  815. $newlyMapped = false;
  816. $uid = $idsByDn[$record['dn'][0]] ?? null;
  817. if ($uid === null) {
  818. $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record);
  819. }
  820. if (is_string($uid)) {
  821. $this->cacheUserExists($uid);
  822. }
  823. return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);
  824. });
  825. }
  826. $this->batchApplyUserAttributes($recordsToUpdate);
  827. return $this->fetchList($ldapRecords, $this->manyAttributes($attr));
  828. }
  829. /**
  830. * provided with an array of LDAP user records the method will fetch the
  831. * user object and requests it to process the freshly fetched attributes and
  832. * and their values
  833. *
  834. * @throws \Exception
  835. */
  836. public function batchApplyUserAttributes(array $ldapRecords): void {
  837. $displayNameAttribute = strtolower((string)$this->connection->ldapUserDisplayName);
  838. foreach ($ldapRecords as $userRecord) {
  839. if (!isset($userRecord[$displayNameAttribute])) {
  840. // displayName is obligatory
  841. continue;
  842. }
  843. $ocName = $this->dn2ocname($userRecord['dn'][0], null, true);
  844. if ($ocName === false) {
  845. continue;
  846. }
  847. $this->updateUserState($ocName);
  848. $user = $this->userManager->get($ocName);
  849. if ($user !== null) {
  850. $user->processAttributes($userRecord);
  851. } else {
  852. $this->logger->debug(
  853. "The ldap user manager returned null for $ocName",
  854. ['app' => 'user_ldap']
  855. );
  856. }
  857. }
  858. }
  859. /**
  860. * @return array[]
  861. */
  862. public function fetchListOfGroups(string $filter, array $attr, int $limit = null, int $offset = null): array {
  863. $cacheKey = 'fetchListOfGroups_' . $filter . '_' . implode('-', $attr) . '_' . (string)$limit . '_' . (string)$offset;
  864. $listOfGroups = $this->connection->getFromCache($cacheKey);
  865. if (!is_null($listOfGroups)) {
  866. return $listOfGroups;
  867. }
  868. $groupRecords = $this->searchGroups($filter, $attr, $limit, $offset);
  869. $listOfDNs = array_reduce($groupRecords, function ($listOfDNs, $entry) {
  870. $listOfDNs[] = $entry['dn'][0];
  871. return $listOfDNs;
  872. }, []);
  873. $idsByDn = $this->getGroupMapper()->getListOfIdsByDn($listOfDNs);
  874. array_walk($groupRecords, function (array $record) use ($idsByDn) {
  875. $newlyMapped = false;
  876. $gid = $idsByDn[$record['dn'][0]] ?? null;
  877. if ($gid === null) {
  878. $gid = $this->dn2ocname($record['dn'][0], null, false, $newlyMapped, $record);
  879. }
  880. if (!$newlyMapped && is_string($gid)) {
  881. $this->cacheGroupExists($gid);
  882. }
  883. });
  884. $listOfGroups = $this->fetchList($groupRecords, $this->manyAttributes($attr));
  885. $this->connection->writeToCache($cacheKey, $listOfGroups);
  886. return $listOfGroups;
  887. }
  888. private function fetchList(array $list, bool $manyAttributes): array {
  889. if ($manyAttributes) {
  890. return $list;
  891. } else {
  892. $list = array_reduce($list, function ($carry, $item) {
  893. $attribute = array_keys($item)[0];
  894. $carry[] = $item[$attribute][0];
  895. return $carry;
  896. }, []);
  897. return array_unique($list, SORT_LOCALE_STRING);
  898. }
  899. }
  900. /**
  901. * @throws ServerNotAvailableException
  902. */
  903. public function searchUsers(string $filter, array $attr = null, int $limit = null, int $offset = null): array {
  904. $result = [];
  905. foreach ($this->connection->ldapBaseUsers as $base) {
  906. $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset));
  907. }
  908. return $result;
  909. }
  910. /**
  911. * @param string[] $attr
  912. * @return false|int
  913. * @throws ServerNotAvailableException
  914. */
  915. public function countUsers(string $filter, array $attr = ['dn'], int $limit = null, int $offset = null) {
  916. $result = false;
  917. foreach ($this->connection->ldapBaseUsers as $base) {
  918. $count = $this->count($filter, [$base], $attr, $limit ?? 0, $offset ?? 0);
  919. $result = is_int($count) ? (int)$result + $count : $result;
  920. }
  921. return $result;
  922. }
  923. /**
  924. * executes an LDAP search, optimized for Groups
  925. *
  926. * @param ?string[] $attr optional, when certain attributes shall be filtered out
  927. *
  928. * Executes an LDAP search
  929. * @throws ServerNotAvailableException
  930. */
  931. public function searchGroups(string $filter, array $attr = null, int $limit = null, int $offset = null): array {
  932. $result = [];
  933. foreach ($this->connection->ldapBaseGroups as $base) {
  934. $result = array_merge($result, $this->search($filter, $base, $attr, $limit, $offset));
  935. }
  936. return $result;
  937. }
  938. /**
  939. * returns the number of available groups
  940. *
  941. * @return int|bool
  942. * @throws ServerNotAvailableException
  943. */
  944. public function countGroups(string $filter, array $attr = ['dn'], int $limit = null, int $offset = null) {
  945. $result = false;
  946. foreach ($this->connection->ldapBaseGroups as $base) {
  947. $count = $this->count($filter, [$base], $attr, $limit ?? 0, $offset ?? 0);
  948. $result = is_int($count) ? (int)$result + $count : $result;
  949. }
  950. return $result;
  951. }
  952. /**
  953. * returns the number of available objects on the base DN
  954. *
  955. * @return int|bool
  956. * @throws ServerNotAvailableException
  957. */
  958. public function countObjects(int $limit = null, int $offset = null) {
  959. $result = false;
  960. foreach ($this->connection->ldapBase as $base) {
  961. $count = $this->count('objectclass=*', [$base], ['dn'], $limit ?? 0, $offset ?? 0);
  962. $result = is_int($count) ? (int)$result + $count : $result;
  963. }
  964. return $result;
  965. }
  966. /**
  967. * Returns the LDAP handler
  968. *
  969. * @throws \OC\ServerNotAvailableException
  970. */
  971. /**
  972. * @param mixed[] $arguments
  973. * @return mixed
  974. * @throws \OC\ServerNotAvailableException
  975. */
  976. private function invokeLDAPMethod(string $command, ...$arguments) {
  977. if ($command == 'controlPagedResultResponse') {
  978. // php no longer supports call-time pass-by-reference
  979. // thus cannot support controlPagedResultResponse as the third argument
  980. // is a reference
  981. throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
  982. }
  983. if (!method_exists($this->ldap, $command)) {
  984. return null;
  985. }
  986. array_unshift($arguments, $this->connection->getConnectionResource());
  987. $doMethod = function () use ($command, &$arguments) {
  988. return call_user_func_array([$this->ldap, $command], $arguments);
  989. };
  990. try {
  991. $ret = $doMethod();
  992. } catch (ServerNotAvailableException $e) {
  993. /* Server connection lost, attempt to reestablish it
  994. * Maybe implement exponential backoff?
  995. * This was enough to get solr indexer working which has large delays between LDAP fetches.
  996. */
  997. $this->logger->debug("Connection lost on $command, attempting to reestablish.", ['app' => 'user_ldap']);
  998. $this->connection->resetConnectionResource();
  999. $cr = $this->connection->getConnectionResource();
  1000. if (!$this->ldap->isResource($cr)) {
  1001. // Seems like we didn't find any resource.
  1002. $this->logger->debug("Could not $command, because resource is missing.", ['app' => 'user_ldap']);
  1003. throw $e;
  1004. }
  1005. $arguments[0] = $cr;
  1006. $ret = $doMethod();
  1007. }
  1008. return $ret;
  1009. }
  1010. /**
  1011. * retrieved. Results will according to the order in the array.
  1012. *
  1013. * @param string $filter
  1014. * @param string $base
  1015. * @param string[] $attr
  1016. * @param int|null $limit optional, maximum results to be counted
  1017. * @param int|null $offset optional, a starting point
  1018. * @return array|false array with the search result as first value and pagedSearchOK as
  1019. * second | false if not successful
  1020. * @throws ServerNotAvailableException
  1021. */
  1022. private function executeSearch(
  1023. string $filter,
  1024. string $base,
  1025. ?array &$attr,
  1026. ?int $pageSize,
  1027. ?int $offset
  1028. ) {
  1029. // See if we have a resource, in case not cancel with message
  1030. $cr = $this->connection->getConnectionResource();
  1031. if (!$this->ldap->isResource($cr)) {
  1032. // Seems like we didn't find any resource.
  1033. // Return an empty array just like before.
  1034. $this->logger->debug('Could not search, because resource is missing.', ['app' => 'user_ldap']);
  1035. return false;
  1036. }
  1037. //check whether paged search should be attempted
  1038. try {
  1039. [$pagedSearchOK, $pageSize, $cookie] = $this->initPagedSearch($filter, $base, $attr, (int)$pageSize, (int)$offset);
  1040. } catch (NoMoreResults $e) {
  1041. // beyond last results page
  1042. return false;
  1043. }
  1044. $sr = $this->invokeLDAPMethod('search', $base, $filter, $attr, 0, 0, $pageSize, $cookie);
  1045. $error = $this->ldap->errno($this->connection->getConnectionResource());
  1046. if (!$this->ldap->isResource($sr) || $error !== 0) {
  1047. $this->logger->error('Attempt for Paging? ' . print_r($pagedSearchOK, true), ['app' => 'user_ldap']);
  1048. return false;
  1049. }
  1050. return [$sr, $pagedSearchOK];
  1051. }
  1052. /**
  1053. * processes an LDAP paged search operation
  1054. *
  1055. * @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr the array containing the LDAP search resources
  1056. * @param int $foundItems number of results in the single search operation
  1057. * @param int $limit maximum results to be counted
  1058. * @param bool $pagedSearchOK whether a paged search has been executed
  1059. * @param bool $skipHandling required for paged search when cookies to
  1060. * prior results need to be gained
  1061. * @return bool cookie validity, true if we have more pages, false otherwise.
  1062. * @throws ServerNotAvailableException
  1063. */
  1064. private function processPagedSearchStatus(
  1065. $sr,
  1066. int $foundItems,
  1067. int $limit,
  1068. bool $pagedSearchOK,
  1069. bool $skipHandling
  1070. ): bool {
  1071. $cookie = '';
  1072. if ($pagedSearchOK) {
  1073. $cr = $this->connection->getConnectionResource();
  1074. if ($this->ldap->controlPagedResultResponse($cr, $sr, $cookie)) {
  1075. $this->lastCookie = $cookie;
  1076. }
  1077. //browsing through prior pages to get the cookie for the new one
  1078. if ($skipHandling) {
  1079. return false;
  1080. }
  1081. // if count is bigger, then the server does not support
  1082. // paged search. Instead, he did a normal search. We set a
  1083. // flag here, so the callee knows how to deal with it.
  1084. if ($foundItems <= $limit) {
  1085. $this->pagedSearchedSuccessful = true;
  1086. }
  1087. } else {
  1088. if ((int)$this->connection->ldapPagingSize !== 0) {
  1089. $this->logger->debug(
  1090. 'Paged search was not available',
  1091. ['app' => 'user_ldap']
  1092. );
  1093. }
  1094. }
  1095. /* ++ Fixing RHDS searches with pages with zero results ++
  1096. * Return cookie status. If we don't have more pages, with RHDS
  1097. * cookie is null, with openldap cookie is an empty string and
  1098. * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0
  1099. */
  1100. return !empty($cookie) || $cookie === '0';
  1101. }
  1102. /**
  1103. * executes an LDAP search, but counts the results only
  1104. *
  1105. * @param string $filter the LDAP filter for the search
  1106. * @param array $bases an array containing the LDAP subtree(s) that shall be searched
  1107. * @param ?string[] $attr optional, array, one or more attributes that shall be
  1108. * retrieved. Results will according to the order in the array.
  1109. * @param int $limit maximum results to be counted, 0 means no limit
  1110. * @param int $offset a starting point, defaults to 0
  1111. * @param bool $skipHandling indicates whether the pages search operation is
  1112. * completed
  1113. * @return int|false Integer or false if the search could not be initialized
  1114. * @throws ServerNotAvailableException
  1115. */
  1116. private function count(
  1117. string $filter,
  1118. array $bases,
  1119. array $attr = null,
  1120. int $limit = 0,
  1121. int $offset = 0,
  1122. bool $skipHandling = false
  1123. ) {
  1124. $this->logger->debug('Count filter: {filter}', [
  1125. 'app' => 'user_ldap',
  1126. 'filter' => $filter
  1127. ]);
  1128. $limitPerPage = (int)$this->connection->ldapPagingSize;
  1129. if ($limit < $limitPerPage && $limit > 0) {
  1130. $limitPerPage = $limit;
  1131. }
  1132. $counter = 0;
  1133. $count = null;
  1134. $this->connection->getConnectionResource();
  1135. foreach ($bases as $base) {
  1136. do {
  1137. $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
  1138. if ($search === false) {
  1139. return $counter > 0 ? $counter : false;
  1140. }
  1141. [$sr, $pagedSearchOK] = $search;
  1142. /* ++ Fixing RHDS searches with pages with zero results ++
  1143. * countEntriesInSearchResults() method signature changed
  1144. * by removing $limit and &$hasHitLimit parameters
  1145. */
  1146. $count = $this->countEntriesInSearchResults($sr);
  1147. $counter += $count;
  1148. $hasMorePages = $this->processPagedSearchStatus($sr, $count, $limitPerPage, $pagedSearchOK, $skipHandling);
  1149. $offset += $limitPerPage;
  1150. /* ++ Fixing RHDS searches with pages with zero results ++
  1151. * Continue now depends on $hasMorePages value
  1152. */
  1153. $continue = $pagedSearchOK && $hasMorePages;
  1154. } while ($continue && ($limit <= 0 || $limit > $counter));
  1155. }
  1156. return $counter;
  1157. }
  1158. /**
  1159. * @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr
  1160. * @return int
  1161. * @throws ServerNotAvailableException
  1162. */
  1163. private function countEntriesInSearchResults($sr): int {
  1164. return (int)$this->invokeLDAPMethod('countEntries', $sr);
  1165. }
  1166. /**
  1167. * Executes an LDAP search
  1168. *
  1169. * @throws ServerNotAvailableException
  1170. */
  1171. public function search(
  1172. string $filter,
  1173. string $base,
  1174. ?array $attr = null,
  1175. ?int $limit = null,
  1176. ?int $offset = null,
  1177. bool $skipHandling = false
  1178. ): array {
  1179. $limitPerPage = (int)$this->connection->ldapPagingSize;
  1180. if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
  1181. $limitPerPage = $limit;
  1182. }
  1183. /* ++ Fixing RHDS searches with pages with zero results ++
  1184. * As we can have pages with zero results and/or pages with less
  1185. * than $limit results but with a still valid server 'cookie',
  1186. * loops through until we get $continue equals true and
  1187. * $findings['count'] < $limit
  1188. */
  1189. $findings = [];
  1190. $offset = $offset ?? 0;
  1191. $savedoffset = $offset;
  1192. $iFoundItems = 0;
  1193. do {
  1194. $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
  1195. if ($search === false) {
  1196. return [];
  1197. }
  1198. [$sr, $pagedSearchOK] = $search;
  1199. if ($skipHandling) {
  1200. //i.e. result do not need to be fetched, we just need the cookie
  1201. //thus pass 1 or any other value as $iFoundItems because it is not
  1202. //used
  1203. $this->processPagedSearchStatus($sr, 1, $limitPerPage, $pagedSearchOK, $skipHandling);
  1204. return [];
  1205. }
  1206. $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $sr));
  1207. $iFoundItems = max($iFoundItems, $findings['count']);
  1208. unset($findings['count']);
  1209. $continue = $this->processPagedSearchStatus($sr, $iFoundItems, $limitPerPage, $pagedSearchOK, $skipHandling);
  1210. $offset += $limitPerPage;
  1211. } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit));
  1212. // resetting offset
  1213. $offset = $savedoffset;
  1214. if (!is_null($attr)) {
  1215. $selection = [];
  1216. $i = 0;
  1217. foreach ($findings as $item) {
  1218. if (!is_array($item)) {
  1219. continue;
  1220. }
  1221. $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');
  1222. foreach ($attr as $key) {
  1223. if (isset($item[$key])) {
  1224. if (is_array($item[$key]) && isset($item[$key]['count'])) {
  1225. unset($item[$key]['count']);
  1226. }
  1227. if ($key !== 'dn') {
  1228. if ($this->resemblesDN($key)) {
  1229. $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]);
  1230. } elseif ($key === 'objectguid' || $key === 'guid') {
  1231. $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])];
  1232. } else {
  1233. $selection[$i][$key] = $item[$key];
  1234. }
  1235. } else {
  1236. $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])];
  1237. }
  1238. }
  1239. }
  1240. $i++;
  1241. }
  1242. $findings = $selection;
  1243. }
  1244. //we slice the findings, when
  1245. //a) paged search unsuccessful, though attempted
  1246. //b) no paged search, but limit set
  1247. if ((!$this->getPagedSearchResultState()
  1248. && $pagedSearchOK)
  1249. || (
  1250. !$pagedSearchOK
  1251. && !is_null($limit)
  1252. )
  1253. ) {
  1254. $findings = array_slice($findings, $offset, $limit);
  1255. }
  1256. return $findings;
  1257. }
  1258. /**
  1259. * @param string $name
  1260. * @return string
  1261. * @throws \InvalidArgumentException
  1262. */
  1263. public function sanitizeUsername($name) {
  1264. $name = trim($name);
  1265. if ($this->connection->ldapIgnoreNamingRules) {
  1266. return $name;
  1267. }
  1268. // Use htmlentities to get rid of accents
  1269. $name = htmlentities($name, ENT_NOQUOTES, 'UTF-8');
  1270. // Remove accents
  1271. $name = preg_replace('#&([A-Za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $name);
  1272. // Remove ligatures
  1273. $name = preg_replace('#&([A-Za-z]{2})(?:lig);#', '\1', $name);
  1274. // Remove unknown leftover entities
  1275. $name = preg_replace('#&[^;]+;#', '', $name);
  1276. // Replacements
  1277. $name = str_replace(' ', '_', $name);
  1278. // Every remaining disallowed characters will be removed
  1279. $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name);
  1280. if (strlen($name) > 64) {
  1281. $name = hash('sha256', $name, false);
  1282. }
  1283. if ($name === '') {
  1284. throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters');
  1285. }
  1286. return $name;
  1287. }
  1288. public function sanitizeGroupIDCandidate(string $candidate): string {
  1289. $candidate = trim($candidate);
  1290. if (strlen($candidate) > 64) {
  1291. $candidate = hash('sha256', $candidate, false);
  1292. }
  1293. if ($candidate === '') {
  1294. throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters');
  1295. }
  1296. return $candidate;
  1297. }
  1298. /**
  1299. * escapes (user provided) parts for LDAP filter
  1300. *
  1301. * @param string $input , the provided value
  1302. * @param bool $allowAsterisk whether in * at the beginning should be preserved
  1303. * @return string the escaped string
  1304. */
  1305. public function escapeFilterPart($input, $allowAsterisk = false): string {
  1306. $asterisk = '';
  1307. if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') {
  1308. $asterisk = '*';
  1309. $input = mb_substr($input, 1, null, 'UTF-8');
  1310. }
  1311. return $asterisk . ldap_escape($input, '', LDAP_ESCAPE_FILTER);
  1312. }
  1313. /**
  1314. * combines the input filters with AND
  1315. *
  1316. * @param string[] $filters the filters to connect
  1317. * @return string the combined filter
  1318. */
  1319. public function combineFilterWithAnd($filters): string {
  1320. return $this->combineFilter($filters, '&');
  1321. }
  1322. /**
  1323. * combines the input filters with OR
  1324. *
  1325. * @param string[] $filters the filters to connect
  1326. * @return string the combined filter
  1327. * Combines Filter arguments with OR
  1328. */
  1329. public function combineFilterWithOr($filters) {
  1330. return $this->combineFilter($filters, '|');
  1331. }
  1332. /**
  1333. * combines the input filters with given operator
  1334. *
  1335. * @param string[] $filters the filters to connect
  1336. * @param string $operator either & or |
  1337. * @return string the combined filter
  1338. */
  1339. private function combineFilter(array $filters, string $operator): string {
  1340. $combinedFilter = '(' . $operator;
  1341. foreach ($filters as $filter) {
  1342. if ($filter !== '' && $filter[0] !== '(') {
  1343. $filter = '(' . $filter . ')';
  1344. }
  1345. $combinedFilter .= $filter;
  1346. }
  1347. $combinedFilter .= ')';
  1348. return $combinedFilter;
  1349. }
  1350. /**
  1351. * creates a filter part for to perform search for users
  1352. *
  1353. * @param string $search the search term
  1354. * @return string the final filter part to use in LDAP searches
  1355. */
  1356. public function getFilterPartForUserSearch($search): string {
  1357. return $this->getFilterPartForSearch($search,
  1358. $this->connection->ldapAttributesForUserSearch,
  1359. $this->connection->ldapUserDisplayName);
  1360. }
  1361. /**
  1362. * creates a filter part for to perform search for groups
  1363. *
  1364. * @param string $search the search term
  1365. * @return string the final filter part to use in LDAP searches
  1366. */
  1367. public function getFilterPartForGroupSearch($search): string {
  1368. return $this->getFilterPartForSearch($search,
  1369. $this->connection->ldapAttributesForGroupSearch,
  1370. $this->connection->ldapGroupDisplayName);
  1371. }
  1372. /**
  1373. * creates a filter part for searches by splitting up the given search
  1374. * string into single words
  1375. *
  1376. * @param string $search the search term
  1377. * @param string[]|null|'' $searchAttributes needs to have at least two attributes,
  1378. * otherwise it does not make sense :)
  1379. * @return string the final filter part to use in LDAP searches
  1380. * @throws DomainException
  1381. */
  1382. private function getAdvancedFilterPartForSearch(string $search, $searchAttributes): string {
  1383. if (!is_array($searchAttributes) || count($searchAttributes) < 2) {
  1384. throw new DomainException('searchAttributes must be an array with at least two string');
  1385. }
  1386. $searchWords = explode(' ', trim($search));
  1387. $wordFilters = [];
  1388. foreach ($searchWords as $word) {
  1389. $word = $this->prepareSearchTerm($word);
  1390. //every word needs to appear at least once
  1391. $wordMatchOneAttrFilters = [];
  1392. foreach ($searchAttributes as $attr) {
  1393. $wordMatchOneAttrFilters[] = $attr . '=' . $word;
  1394. }
  1395. $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);
  1396. }
  1397. return $this->combineFilterWithAnd($wordFilters);
  1398. }
  1399. /**
  1400. * creates a filter part for searches
  1401. *
  1402. * @param string $search the search term
  1403. * @param string[]|null|'' $searchAttributes
  1404. * @param string $fallbackAttribute a fallback attribute in case the user
  1405. * did not define search attributes. Typically the display name attribute.
  1406. * @return string the final filter part to use in LDAP searches
  1407. */
  1408. private function getFilterPartForSearch(string $search, $searchAttributes, string $fallbackAttribute): string {
  1409. $filter = [];
  1410. $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);
  1411. if ($haveMultiSearchAttributes && str_contains(trim($search), ' ')) {
  1412. try {
  1413. return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);
  1414. } catch (DomainException $e) {
  1415. // Creating advanced filter for search failed, falling back to simple method. Edge case, but valid.
  1416. }
  1417. }
  1418. $originalSearch = $search;
  1419. $search = $this->prepareSearchTerm($search);
  1420. if (!is_array($searchAttributes) || count($searchAttributes) === 0) {
  1421. if ($fallbackAttribute === '') {
  1422. return '';
  1423. }
  1424. // wildcards don't work with some attributes
  1425. if ($originalSearch !== '') {
  1426. $filter[] = $fallbackAttribute . '=' . $originalSearch;
  1427. }
  1428. $filter[] = $fallbackAttribute . '=' . $search;
  1429. } else {
  1430. foreach ($searchAttributes as $attribute) {
  1431. // wildcards don't work with some attributes
  1432. if ($originalSearch !== '') {
  1433. $filter[] = $attribute . '=' . $originalSearch;
  1434. }
  1435. $filter[] = $attribute . '=' . $search;
  1436. }
  1437. }
  1438. if (count($filter) === 1) {
  1439. return '(' . $filter[0] . ')';
  1440. }
  1441. return $this->combineFilterWithOr($filter);
  1442. }
  1443. /**
  1444. * returns the search term depending on whether we are allowed
  1445. * list users found by ldap with the current input appended by
  1446. * a *
  1447. */
  1448. private function prepareSearchTerm(string $term): string {
  1449. $config = \OC::$server->getConfig();
  1450. $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes');
  1451. $result = $term;
  1452. if ($term === '') {
  1453. $result = '*';
  1454. } elseif ($allowEnum !== 'no') {
  1455. $result = $term . '*';
  1456. }
  1457. return $result;
  1458. }
  1459. /**
  1460. * returns the filter used for counting users
  1461. */
  1462. public function getFilterForUserCount(): string {
  1463. $filter = $this->combineFilterWithAnd([
  1464. $this->connection->ldapUserFilter,
  1465. $this->connection->ldapUserDisplayName . '=*'
  1466. ]);
  1467. return $filter;
  1468. }
  1469. /**
  1470. * @param string $name
  1471. * @param string $password
  1472. * @return bool
  1473. */
  1474. public function areCredentialsValid($name, $password) {
  1475. $name = $this->helper->DNasBaseParameter($name);
  1476. $testConnection = clone $this->connection;
  1477. $credentials = [
  1478. 'ldapAgentName' => $name,
  1479. 'ldapAgentPassword' => $password
  1480. ];
  1481. if (!$testConnection->setConfiguration($credentials)) {
  1482. return false;
  1483. }
  1484. return $testConnection->bind();
  1485. }
  1486. /**
  1487. * reverse lookup of a DN given a known UUID
  1488. *
  1489. * @param string $uuid
  1490. * @return string
  1491. * @throws \Exception
  1492. */
  1493. public function getUserDnByUuid($uuid) {
  1494. $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
  1495. $filter = $this->connection->ldapUserFilter;
  1496. $bases = $this->connection->ldapBaseUsers;
  1497. if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') {
  1498. // Sacrebleu! The UUID attribute is unknown :( We need first an
  1499. // existing DN to be able to reliably detect it.
  1500. foreach ($bases as $base) {
  1501. $result = $this->search($filter, $base, ['dn'], 1);
  1502. if (!isset($result[0]) || !isset($result[0]['dn'])) {
  1503. continue;
  1504. }
  1505. $dn = $result[0]['dn'][0];
  1506. if ($hasFound = $this->detectUuidAttribute($dn, true)) {
  1507. break;
  1508. }
  1509. }
  1510. if (!isset($hasFound) || !$hasFound) {
  1511. throw new \Exception('Cannot determine UUID attribute');
  1512. }
  1513. } else {
  1514. // The UUID attribute is either known or an override is given.
  1515. // By calling this method we ensure that $this->connection->$uuidAttr
  1516. // is definitely set
  1517. if (!$this->detectUuidAttribute('', true)) {
  1518. throw new \Exception('Cannot determine UUID attribute');
  1519. }
  1520. }
  1521. $uuidAttr = $this->connection->ldapUuidUserAttribute;
  1522. if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') {
  1523. $uuid = $this->formatGuid2ForFilterUser($uuid);
  1524. }
  1525. $filter = $uuidAttr . '=' . $uuid;
  1526. $result = $this->searchUsers($filter, ['dn'], 2);
  1527. if (isset($result[0]['dn']) && count($result) === 1) {
  1528. // we put the count into account to make sure that this is
  1529. // really unique
  1530. return $result[0]['dn'][0];
  1531. }
  1532. throw new \Exception('Cannot determine UUID attribute');
  1533. }
  1534. /**
  1535. * auto-detects the directory's UUID attribute
  1536. *
  1537. * @param string $dn a known DN used to check against
  1538. * @param bool $isUser
  1539. * @param bool $force the detection should be run, even if it is not set to auto
  1540. * @param array|null $ldapRecord
  1541. * @return bool true on success, false otherwise
  1542. * @throws ServerNotAvailableException
  1543. */
  1544. private function detectUuidAttribute(string $dn, bool $isUser = true, bool $force = false, ?array $ldapRecord = null): bool {
  1545. if ($isUser) {
  1546. $uuidAttr = 'ldapUuidUserAttribute';
  1547. $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
  1548. } else {
  1549. $uuidAttr = 'ldapUuidGroupAttribute';
  1550. $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
  1551. }
  1552. if (!$force) {
  1553. if ($this->connection->$uuidAttr !== 'auto') {
  1554. return true;
  1555. } elseif (is_string($uuidOverride) && trim($uuidOverride) !== '') {
  1556. $this->connection->$uuidAttr = $uuidOverride;
  1557. return true;
  1558. }
  1559. $attribute = $this->connection->getFromCache($uuidAttr);
  1560. if ($attribute !== null) {
  1561. $this->connection->$uuidAttr = $attribute;
  1562. return true;
  1563. }
  1564. }
  1565. foreach (self::UUID_ATTRIBUTES as $attribute) {
  1566. if ($ldapRecord !== null) {
  1567. // we have the info from LDAP already, we don't need to talk to the server again
  1568. if (isset($ldapRecord[$attribute])) {
  1569. $this->connection->$uuidAttr = $attribute;
  1570. return true;
  1571. }
  1572. }
  1573. $value = $this->readAttribute($dn, $attribute);
  1574. if (is_array($value) && isset($value[0]) && !empty($value[0])) {
  1575. $this->logger->debug(
  1576. 'Setting {attribute} as {subject}',
  1577. [
  1578. 'app' => 'user_ldap',
  1579. 'attribute' => $attribute,
  1580. 'subject' => $uuidAttr
  1581. ]
  1582. );
  1583. $this->connection->$uuidAttr = $attribute;
  1584. $this->connection->writeToCache($uuidAttr, $attribute);
  1585. return true;
  1586. }
  1587. }
  1588. $this->logger->debug('Could not autodetect the UUID attribute', ['app' => 'user_ldap']);
  1589. return false;
  1590. }
  1591. /**
  1592. * @param array|null $ldapRecord
  1593. * @return false|string
  1594. * @throws ServerNotAvailableException
  1595. */
  1596. public function getUUID(string $dn, bool $isUser = true, array $ldapRecord = null) {
  1597. if ($isUser) {
  1598. $uuidAttr = 'ldapUuidUserAttribute';
  1599. $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
  1600. } else {
  1601. $uuidAttr = 'ldapUuidGroupAttribute';
  1602. $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
  1603. }
  1604. $uuid = false;
  1605. if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) {
  1606. $attr = $this->connection->$uuidAttr;
  1607. $uuid = $ldapRecord[$attr] ?? $this->readAttribute($dn, $attr);
  1608. if (!is_array($uuid)
  1609. && $uuidOverride !== ''
  1610. && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) {
  1611. $uuid = isset($ldapRecord[$this->connection->$uuidAttr])
  1612. ? $ldapRecord[$this->connection->$uuidAttr]
  1613. : $this->readAttribute($dn, $this->connection->$uuidAttr);
  1614. }
  1615. if (is_array($uuid) && !empty($uuid[0])) {
  1616. $uuid = $uuid[0];
  1617. }
  1618. }
  1619. return $uuid;
  1620. }
  1621. /**
  1622. * converts a binary ObjectGUID into a string representation
  1623. *
  1624. * @param string $oguid the ObjectGUID in its binary form as retrieved from AD
  1625. * @link https://www.php.net/manual/en/function.ldap-get-values-len.php#73198
  1626. */
  1627. private function convertObjectGUID2Str(string $oguid): string {
  1628. $hex_guid = bin2hex($oguid);
  1629. $hex_guid_to_guid_str = '';
  1630. for ($k = 1; $k <= 4; ++$k) {
  1631. $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
  1632. }
  1633. $hex_guid_to_guid_str .= '-';
  1634. for ($k = 1; $k <= 2; ++$k) {
  1635. $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
  1636. }
  1637. $hex_guid_to_guid_str .= '-';
  1638. for ($k = 1; $k <= 2; ++$k) {
  1639. $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
  1640. }
  1641. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
  1642. $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
  1643. return strtoupper($hex_guid_to_guid_str);
  1644. }
  1645. /**
  1646. * the first three blocks of the string-converted GUID happen to be in
  1647. * reverse order. In order to use it in a filter, this needs to be
  1648. * corrected. Furthermore the dashes need to be replaced and \\ prepended
  1649. * to every two hex figures.
  1650. *
  1651. * If an invalid string is passed, it will be returned without change.
  1652. */
  1653. public function formatGuid2ForFilterUser(string $guid): string {
  1654. $blocks = explode('-', $guid);
  1655. if (count($blocks) !== 5) {
  1656. /*
  1657. * Why not throw an Exception instead? This method is a utility
  1658. * called only when trying to figure out whether a "missing" known
  1659. * LDAP user was or was not renamed on the LDAP server. And this
  1660. * even on the use case that a reverse lookup is needed (UUID known,
  1661. * not DN), i.e. when finding users (search dialog, users page,
  1662. * login, …) this will not be fired. This occurs only if shares from
  1663. * a users are supposed to be mounted who cannot be found. Throwing
  1664. * an exception here would kill the experience for a valid, acting
  1665. * user. Instead we write a log message.
  1666. */
  1667. $this->logger->info(
  1668. 'Passed string does not resemble a valid GUID. Known UUID ' .
  1669. '({uuid}) probably does not match UUID configuration.',
  1670. ['app' => 'user_ldap', 'uuid' => $guid]
  1671. );
  1672. return $guid;
  1673. }
  1674. for ($i = 0; $i < 3; $i++) {
  1675. $pairs = str_split($blocks[$i], 2);
  1676. $pairs = array_reverse($pairs);
  1677. $blocks[$i] = implode('', $pairs);
  1678. }
  1679. for ($i = 0; $i < 5; $i++) {
  1680. $pairs = str_split($blocks[$i], 2);
  1681. $blocks[$i] = '\\' . implode('\\', $pairs);
  1682. }
  1683. return implode('', $blocks);
  1684. }
  1685. /**
  1686. * gets a SID of the domain of the given dn
  1687. *
  1688. * @param string $dn
  1689. * @return string|bool
  1690. * @throws ServerNotAvailableException
  1691. */
  1692. public function getSID($dn) {
  1693. $domainDN = $this->getDomainDNFromDN($dn);
  1694. $cacheKey = 'getSID-' . $domainDN;
  1695. $sid = $this->connection->getFromCache($cacheKey);
  1696. if (!is_null($sid)) {
  1697. return $sid;
  1698. }
  1699. $objectSid = $this->readAttribute($domainDN, 'objectsid');
  1700. if (!is_array($objectSid) || empty($objectSid)) {
  1701. $this->connection->writeToCache($cacheKey, false);
  1702. return false;
  1703. }
  1704. $domainObjectSid = $this->convertSID2Str($objectSid[0]);
  1705. $this->connection->writeToCache($cacheKey, $domainObjectSid);
  1706. return $domainObjectSid;
  1707. }
  1708. /**
  1709. * converts a binary SID into a string representation
  1710. *
  1711. * @param string $sid
  1712. * @return string
  1713. */
  1714. public function convertSID2Str($sid) {
  1715. // The format of a SID binary string is as follows:
  1716. // 1 byte for the revision level
  1717. // 1 byte for the number n of variable sub-ids
  1718. // 6 bytes for identifier authority value
  1719. // n*4 bytes for n sub-ids
  1720. //
  1721. // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f
  1722. // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444
  1723. $revision = ord($sid[0]);
  1724. $numberSubID = ord($sid[1]);
  1725. $subIdStart = 8; // 1 + 1 + 6
  1726. $subIdLength = 4;
  1727. if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) {
  1728. // Incorrect number of bytes present.
  1729. return '';
  1730. }
  1731. // 6 bytes = 48 bits can be represented using floats without loss of
  1732. // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71)
  1733. $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', '');
  1734. $subIDs = [];
  1735. for ($i = 0; $i < $numberSubID; $i++) {
  1736. $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength));
  1737. $subIDs[] = sprintf('%u', $subID[1]);
  1738. }
  1739. // Result for example above: S-1-5-21-249921958-728525901-1594176202
  1740. return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs));
  1741. }
  1742. /**
  1743. * checks if the given DN is part of the given base DN(s)
  1744. *
  1745. * @param string[] $bases array containing the allowed base DN or DNs
  1746. */
  1747. public function isDNPartOfBase(string $dn, array $bases): bool {
  1748. $belongsToBase = false;
  1749. $bases = $this->helper->sanitizeDN($bases);
  1750. foreach ($bases as $base) {
  1751. $belongsToBase = true;
  1752. if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) {
  1753. $belongsToBase = false;
  1754. }
  1755. if ($belongsToBase) {
  1756. break;
  1757. }
  1758. }
  1759. return $belongsToBase;
  1760. }
  1761. /**
  1762. * resets a running Paged Search operation
  1763. *
  1764. * @throws ServerNotAvailableException
  1765. */
  1766. private function abandonPagedSearch(): void {
  1767. if ($this->lastCookie === '') {
  1768. return;
  1769. }
  1770. $this->getPagedSearchResultState();
  1771. $this->lastCookie = '';
  1772. }
  1773. /**
  1774. * checks whether an LDAP paged search operation has more pages that can be
  1775. * retrieved, typically when offset and limit are provided.
  1776. *
  1777. * Be very careful to use it: the last cookie value, which is inspected, can
  1778. * be reset by other operations. Best, call it immediately after a search(),
  1779. * searchUsers() or searchGroups() call. count-methods are probably safe as
  1780. * well. Don't rely on it with any fetchList-method.
  1781. *
  1782. * @return bool
  1783. */
  1784. public function hasMoreResults() {
  1785. if ($this->lastCookie === '') {
  1786. // as in RFC 2696, when all results are returned, the cookie will
  1787. // be empty.
  1788. return false;
  1789. }
  1790. return true;
  1791. }
  1792. /**
  1793. * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search.
  1794. *
  1795. * @return boolean|null true on success, null or false otherwise
  1796. */
  1797. public function getPagedSearchResultState() {
  1798. $result = $this->pagedSearchedSuccessful;
  1799. $this->pagedSearchedSuccessful = null;
  1800. return $result;
  1801. }
  1802. /**
  1803. * Prepares a paged search, if possible
  1804. *
  1805. * @param string $filter the LDAP filter for the search
  1806. * @param string $base the LDAP subtree that shall be searched
  1807. * @param string[] $attr optional, when a certain attribute shall be filtered outside
  1808. * @param int $limit
  1809. * @param int $offset
  1810. * @return array{bool, int, string}
  1811. * @throws ServerNotAvailableException
  1812. * @throws NoMoreResults
  1813. */
  1814. private function initPagedSearch(
  1815. string $filter,
  1816. string $base,
  1817. ?array $attr,
  1818. int $pageSize,
  1819. int $offset
  1820. ): array {
  1821. $pagedSearchOK = false;
  1822. if ($pageSize !== 0) {
  1823. $this->logger->debug(
  1824. 'initializing paged search for filter {filter}, base {base}, attr {attr}, pageSize {pageSize}, offset {offset}',
  1825. [
  1826. 'app' => 'user_ldap',
  1827. 'filter' => $filter,
  1828. 'base' => $base,
  1829. 'attr' => $attr,
  1830. 'pageSize' => $pageSize,
  1831. 'offset' => $offset
  1832. ]
  1833. );
  1834. // Get the cookie from the search for the previous search, required by LDAP
  1835. if (($this->lastCookie === '') && ($offset > 0)) {
  1836. // no cookie known from a potential previous search. We need
  1837. // to start from 0 to come to the desired page. cookie value
  1838. // of '0' is valid, because 389ds
  1839. $defaultPageSize = (int)$this->connection->ldapPagingSize;
  1840. if ($offset < $defaultPageSize) {
  1841. /* Make a search with offset as page size and dismiss the result, to init the cookie */
  1842. $this->search($filter, $base, $attr, $offset, 0, true);
  1843. } else {
  1844. /* Make a search for previous page and dismiss the result, to init the cookie */
  1845. $reOffset = $offset - $defaultPageSize;
  1846. $this->search($filter, $base, $attr, $defaultPageSize, $reOffset, true);
  1847. }
  1848. if (!$this->hasMoreResults()) {
  1849. // when the cookie is reset with != 0 offset, there are no further
  1850. // results, so stop.
  1851. throw new NoMoreResults();
  1852. }
  1853. }
  1854. if ($this->lastCookie !== '' && $offset === 0) {
  1855. //since offset = 0, this is a new search. We abandon other searches that might be ongoing.
  1856. $this->abandonPagedSearch();
  1857. }
  1858. $this->logger->debug('Ready for a paged search', ['app' => 'user_ldap']);
  1859. return [true, $pageSize, $this->lastCookie];
  1860. /* ++ Fixing RHDS searches with pages with zero results ++
  1861. * We couldn't get paged searches working with our RHDS for login ($limit = 0),
  1862. * due to pages with zero results.
  1863. * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination
  1864. * if we don't have a previous paged search.
  1865. */
  1866. } elseif ($this->lastCookie !== '') {
  1867. // a search without limit was requested. However, if we do use
  1868. // Paged Search once, we always must do it. This requires us to
  1869. // initialize it with the configured page size.
  1870. $this->abandonPagedSearch();
  1871. // in case someone set it to 0 … use 500, otherwise no results will
  1872. // be returned.
  1873. $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500;
  1874. return [true, $pageSize, $this->lastCookie];
  1875. }
  1876. return [false, $pageSize, ''];
  1877. }
  1878. /**
  1879. * Is more than one $attr used for search?
  1880. *
  1881. * @param string|string[]|null $attr
  1882. * @return bool
  1883. */
  1884. private function manyAttributes($attr): bool {
  1885. if (\is_array($attr)) {
  1886. return \count($attr) > 1;
  1887. }
  1888. return false;
  1889. }
  1890. }