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