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