@@ -29,12 +29,12 @@ |
||
29 | 29 | */ |
30 | 30 | class GroupMapping extends AbstractMapping { |
31 | 31 | |
32 | - /** |
|
33 | - * returns the DB table name which holds the mappings |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function getTableName() { |
|
37 | - return '*PREFIX*ldap_group_mapping'; |
|
38 | - } |
|
32 | + /** |
|
33 | + * returns the DB table name which holds the mappings |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function getTableName() { |
|
37 | + return '*PREFIX*ldap_group_mapping'; |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
@@ -29,12 +29,12 @@ |
||
29 | 29 | */ |
30 | 30 | class UserMapping extends AbstractMapping { |
31 | 31 | |
32 | - /** |
|
33 | - * returns the DB table name which holds the mappings |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - protected function getTableName() { |
|
37 | - return '*PREFIX*ldap_user_mapping'; |
|
38 | - } |
|
32 | + /** |
|
33 | + * returns the DB table name which holds the mappings |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + protected function getTableName() { |
|
37 | + return '*PREFIX*ldap_user_mapping'; |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
@@ -29,252 +29,252 @@ |
||
29 | 29 | * @package OCA\User_LDAP\Mapping |
30 | 30 | */ |
31 | 31 | abstract class AbstractMapping { |
32 | - /** |
|
33 | - * @var \OCP\IDBConnection $dbc |
|
34 | - */ |
|
35 | - protected $dbc; |
|
32 | + /** |
|
33 | + * @var \OCP\IDBConnection $dbc |
|
34 | + */ |
|
35 | + protected $dbc; |
|
36 | 36 | |
37 | - /** |
|
38 | - * returns the DB table name which holds the mappings |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - abstract protected function getTableName(); |
|
37 | + /** |
|
38 | + * returns the DB table name which holds the mappings |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + abstract protected function getTableName(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param \OCP\IDBConnection $dbc |
|
45 | - */ |
|
46 | - public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | - $this->dbc = $dbc; |
|
48 | - } |
|
43 | + /** |
|
44 | + * @param \OCP\IDBConnection $dbc |
|
45 | + */ |
|
46 | + public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | + $this->dbc = $dbc; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * checks whether a provided string represents an existing table col |
|
52 | - * @param string $col |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - public function isColNameValid($col) { |
|
56 | - switch($col) { |
|
57 | - case 'ldap_dn': |
|
58 | - case 'owncloud_name': |
|
59 | - case 'directory_uuid': |
|
60 | - return true; |
|
61 | - default: |
|
62 | - return false; |
|
63 | - } |
|
64 | - } |
|
50 | + /** |
|
51 | + * checks whether a provided string represents an existing table col |
|
52 | + * @param string $col |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + public function isColNameValid($col) { |
|
56 | + switch($col) { |
|
57 | + case 'ldap_dn': |
|
58 | + case 'owncloud_name': |
|
59 | + case 'directory_uuid': |
|
60 | + return true; |
|
61 | + default: |
|
62 | + return false; |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Gets the value of one column based on a provided value of another column |
|
68 | - * @param string $fetchCol |
|
69 | - * @param string $compareCol |
|
70 | - * @param string $search |
|
71 | - * @throws \Exception |
|
72 | - * @return string|false |
|
73 | - */ |
|
74 | - protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | - if(!$this->isColNameValid($fetchCol)) { |
|
76 | - //this is used internally only, but we don't want to risk |
|
77 | - //having SQL injection at all. |
|
78 | - throw new \Exception('Invalid Column Name'); |
|
79 | - } |
|
80 | - $query = $this->dbc->prepare(' |
|
66 | + /** |
|
67 | + * Gets the value of one column based on a provided value of another column |
|
68 | + * @param string $fetchCol |
|
69 | + * @param string $compareCol |
|
70 | + * @param string $search |
|
71 | + * @throws \Exception |
|
72 | + * @return string|false |
|
73 | + */ |
|
74 | + protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | + if(!$this->isColNameValid($fetchCol)) { |
|
76 | + //this is used internally only, but we don't want to risk |
|
77 | + //having SQL injection at all. |
|
78 | + throw new \Exception('Invalid Column Name'); |
|
79 | + } |
|
80 | + $query = $this->dbc->prepare(' |
|
81 | 81 | SELECT `' . $fetchCol . '` |
82 | 82 | FROM `'. $this->getTableName() .'` |
83 | 83 | WHERE `' . $compareCol . '` = ? |
84 | 84 | '); |
85 | 85 | |
86 | - $res = $query->execute(array($search)); |
|
87 | - if($res !== false) { |
|
88 | - return $query->fetchColumn(); |
|
89 | - } |
|
86 | + $res = $query->execute(array($search)); |
|
87 | + if($res !== false) { |
|
88 | + return $query->fetchColumn(); |
|
89 | + } |
|
90 | 90 | |
91 | - return false; |
|
92 | - } |
|
91 | + return false; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Performs a DELETE or UPDATE query to the database. |
|
96 | - * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | - * @param array $parameters |
|
98 | - * @return bool true if at least one row was modified, false otherwise |
|
99 | - */ |
|
100 | - protected function modify($query, $parameters) { |
|
101 | - $result = $query->execute($parameters); |
|
102 | - return ($result === true && $query->rowCount() > 0); |
|
103 | - } |
|
94 | + /** |
|
95 | + * Performs a DELETE or UPDATE query to the database. |
|
96 | + * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | + * @param array $parameters |
|
98 | + * @return bool true if at least one row was modified, false otherwise |
|
99 | + */ |
|
100 | + protected function modify($query, $parameters) { |
|
101 | + $result = $query->execute($parameters); |
|
102 | + return ($result === true && $query->rowCount() > 0); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Gets the LDAP DN based on the provided name. |
|
107 | - * Replaces Access::ocname2dn |
|
108 | - * @param string $name |
|
109 | - * @return string|false |
|
110 | - */ |
|
111 | - public function getDNByName($name) { |
|
112 | - return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | - } |
|
105 | + /** |
|
106 | + * Gets the LDAP DN based on the provided name. |
|
107 | + * Replaces Access::ocname2dn |
|
108 | + * @param string $name |
|
109 | + * @return string|false |
|
110 | + */ |
|
111 | + public function getDNByName($name) { |
|
112 | + return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Updates the DN based on the given UUID |
|
117 | - * @param string $fdn |
|
118 | - * @param string $uuid |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public function setDNbyUUID($fdn, $uuid) { |
|
122 | - $query = $this->dbc->prepare(' |
|
115 | + /** |
|
116 | + * Updates the DN based on the given UUID |
|
117 | + * @param string $fdn |
|
118 | + * @param string $uuid |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public function setDNbyUUID($fdn, $uuid) { |
|
122 | + $query = $this->dbc->prepare(' |
|
123 | 123 | UPDATE `' . $this->getTableName() . '` |
124 | 124 | SET `ldap_dn` = ? |
125 | 125 | WHERE `directory_uuid` = ? |
126 | 126 | '); |
127 | 127 | |
128 | - return $this->modify($query, array($fdn, $uuid)); |
|
129 | - } |
|
128 | + return $this->modify($query, array($fdn, $uuid)); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Updates the UUID based on the given DN |
|
133 | - * |
|
134 | - * required by Migration/UUIDFix |
|
135 | - * |
|
136 | - * @param $uuid |
|
137 | - * @param $fdn |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function setUUIDbyDN($uuid, $fdn) { |
|
141 | - $query = $this->dbc->prepare(' |
|
131 | + /** |
|
132 | + * Updates the UUID based on the given DN |
|
133 | + * |
|
134 | + * required by Migration/UUIDFix |
|
135 | + * |
|
136 | + * @param $uuid |
|
137 | + * @param $fdn |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function setUUIDbyDN($uuid, $fdn) { |
|
141 | + $query = $this->dbc->prepare(' |
|
142 | 142 | UPDATE `' . $this->getTableName() . '` |
143 | 143 | SET `directory_uuid` = ? |
144 | 144 | WHERE `ldap_dn` = ? |
145 | 145 | '); |
146 | 146 | |
147 | - return $this->modify($query, [$uuid, $fdn]); |
|
148 | - } |
|
147 | + return $this->modify($query, [$uuid, $fdn]); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Gets the name based on the provided LDAP DN. |
|
152 | - * @param string $fdn |
|
153 | - * @return string|false |
|
154 | - */ |
|
155 | - public function getNameByDN($fdn) { |
|
156 | - return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | - } |
|
150 | + /** |
|
151 | + * Gets the name based on the provided LDAP DN. |
|
152 | + * @param string $fdn |
|
153 | + * @return string|false |
|
154 | + */ |
|
155 | + public function getNameByDN($fdn) { |
|
156 | + return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Searches mapped names by the giving string in the name column |
|
161 | - * @param string $search |
|
162 | - * @param string $prefixMatch |
|
163 | - * @param string $postfixMatch |
|
164 | - * @return string[] |
|
165 | - */ |
|
166 | - public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | - $query = $this->dbc->prepare(' |
|
159 | + /** |
|
160 | + * Searches mapped names by the giving string in the name column |
|
161 | + * @param string $search |
|
162 | + * @param string $prefixMatch |
|
163 | + * @param string $postfixMatch |
|
164 | + * @return string[] |
|
165 | + */ |
|
166 | + public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | + $query = $this->dbc->prepare(' |
|
168 | 168 | SELECT `owncloud_name` |
169 | 169 | FROM `'. $this->getTableName() .'` |
170 | 170 | WHERE `owncloud_name` LIKE ? |
171 | 171 | '); |
172 | 172 | |
173 | - $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | - $names = array(); |
|
175 | - if($res !== false) { |
|
176 | - while($row = $query->fetch()) { |
|
177 | - $names[] = $row['owncloud_name']; |
|
178 | - } |
|
179 | - } |
|
180 | - return $names; |
|
181 | - } |
|
173 | + $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | + $names = array(); |
|
175 | + if($res !== false) { |
|
176 | + while($row = $query->fetch()) { |
|
177 | + $names[] = $row['owncloud_name']; |
|
178 | + } |
|
179 | + } |
|
180 | + return $names; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Gets the name based on the provided LDAP UUID. |
|
185 | - * @param string $uuid |
|
186 | - * @return string|false |
|
187 | - */ |
|
188 | - public function getNameByUUID($uuid) { |
|
189 | - return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | - } |
|
183 | + /** |
|
184 | + * Gets the name based on the provided LDAP UUID. |
|
185 | + * @param string $uuid |
|
186 | + * @return string|false |
|
187 | + */ |
|
188 | + public function getNameByUUID($uuid) { |
|
189 | + return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * Gets the UUID based on the provided LDAP DN |
|
194 | - * @param string $dn |
|
195 | - * @return false|string |
|
196 | - * @throws \Exception |
|
197 | - */ |
|
198 | - public function getUUIDByDN($dn) { |
|
199 | - return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | - } |
|
192 | + /** |
|
193 | + * Gets the UUID based on the provided LDAP DN |
|
194 | + * @param string $dn |
|
195 | + * @return false|string |
|
196 | + * @throws \Exception |
|
197 | + */ |
|
198 | + public function getUUIDByDN($dn) { |
|
199 | + return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * gets a piece of the mapping list |
|
204 | - * @param int $offset |
|
205 | - * @param int $limit |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - public function getList($offset = null, $limit = null) { |
|
209 | - $query = $this->dbc->prepare(' |
|
202 | + /** |
|
203 | + * gets a piece of the mapping list |
|
204 | + * @param int $offset |
|
205 | + * @param int $limit |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + public function getList($offset = null, $limit = null) { |
|
209 | + $query = $this->dbc->prepare(' |
|
210 | 210 | SELECT |
211 | 211 | `ldap_dn` AS `dn`, |
212 | 212 | `owncloud_name` AS `name`, |
213 | 213 | `directory_uuid` AS `uuid` |
214 | 214 | FROM `' . $this->getTableName() . '`', |
215 | - $limit, |
|
216 | - $offset |
|
217 | - ); |
|
215 | + $limit, |
|
216 | + $offset |
|
217 | + ); |
|
218 | 218 | |
219 | - $query->execute(); |
|
220 | - return $query->fetchAll(); |
|
221 | - } |
|
219 | + $query->execute(); |
|
220 | + return $query->fetchAll(); |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * attempts to map the given entry |
|
225 | - * @param string $fdn fully distinguished name (from LDAP) |
|
226 | - * @param string $name |
|
227 | - * @param string $uuid a unique identifier as used in LDAP |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function map($fdn, $name, $uuid) { |
|
231 | - if(mb_strlen($fdn) > 255) { |
|
232 | - \OC::$server->getLogger()->error( |
|
233 | - 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | - [ |
|
235 | - 'app' => 'user_ldap', |
|
236 | - 'dn' => $fdn, |
|
237 | - ] |
|
238 | - ); |
|
239 | - return false; |
|
240 | - } |
|
223 | + /** |
|
224 | + * attempts to map the given entry |
|
225 | + * @param string $fdn fully distinguished name (from LDAP) |
|
226 | + * @param string $name |
|
227 | + * @param string $uuid a unique identifier as used in LDAP |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function map($fdn, $name, $uuid) { |
|
231 | + if(mb_strlen($fdn) > 255) { |
|
232 | + \OC::$server->getLogger()->error( |
|
233 | + 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | + [ |
|
235 | + 'app' => 'user_ldap', |
|
236 | + 'dn' => $fdn, |
|
237 | + ] |
|
238 | + ); |
|
239 | + return false; |
|
240 | + } |
|
241 | 241 | |
242 | - $row = array( |
|
243 | - 'ldap_dn' => $fdn, |
|
244 | - 'owncloud_name' => $name, |
|
245 | - 'directory_uuid' => $uuid |
|
246 | - ); |
|
242 | + $row = array( |
|
243 | + 'ldap_dn' => $fdn, |
|
244 | + 'owncloud_name' => $name, |
|
245 | + 'directory_uuid' => $uuid |
|
246 | + ); |
|
247 | 247 | |
248 | - try { |
|
249 | - $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | - // insertIfNotExist returns values as int |
|
251 | - return (bool)$result; |
|
252 | - } catch (\Exception $e) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - } |
|
248 | + try { |
|
249 | + $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | + // insertIfNotExist returns values as int |
|
251 | + return (bool)$result; |
|
252 | + } catch (\Exception $e) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * removes a mapping based on the owncloud_name of the entry |
|
259 | - * @param string $name |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function unmap($name) { |
|
263 | - $query = $this->dbc->prepare(' |
|
257 | + /** |
|
258 | + * removes a mapping based on the owncloud_name of the entry |
|
259 | + * @param string $name |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function unmap($name) { |
|
263 | + $query = $this->dbc->prepare(' |
|
264 | 264 | DELETE FROM `'. $this->getTableName() .'` |
265 | 265 | WHERE `owncloud_name` = ?'); |
266 | 266 | |
267 | - return $this->modify($query, array($name)); |
|
268 | - } |
|
267 | + return $this->modify($query, array($name)); |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Truncate's the mapping table |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public function clear() { |
|
275 | - $sql = $this->dbc |
|
276 | - ->getDatabasePlatform() |
|
277 | - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | - return $this->dbc->prepare($sql)->execute(); |
|
279 | - } |
|
270 | + /** |
|
271 | + * Truncate's the mapping table |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public function clear() { |
|
275 | + $sql = $this->dbc |
|
276 | + ->getDatabasePlatform() |
|
277 | + ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | + return $this->dbc->prepare($sql)->execute(); |
|
279 | + } |
|
280 | 280 | } |
@@ -27,182 +27,182 @@ |
||
27 | 27 | namespace OCA\User_LDAP; |
28 | 28 | |
29 | 29 | class Group_Proxy extends Proxy implements \OCP\GroupInterface { |
30 | - private $backends = array(); |
|
31 | - private $refBackend = null; |
|
32 | - |
|
33 | - /** |
|
34 | - * Constructor |
|
35 | - * @param string[] $serverConfigPrefixes array containing the config Prefixes |
|
36 | - */ |
|
37 | - public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) { |
|
38 | - parent::__construct($ldap); |
|
39 | - foreach($serverConfigPrefixes as $configPrefix) { |
|
40 | - $this->backends[$configPrefix] = |
|
41 | - new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix)); |
|
42 | - if(is_null($this->refBackend)) { |
|
43 | - $this->refBackend = &$this->backends[$configPrefix]; |
|
44 | - } |
|
45 | - } |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Tries the backends one after the other until a positive result is returned from the specified method |
|
50 | - * @param string $gid the gid connected to the request |
|
51 | - * @param string $method the method of the group backend that shall be called |
|
52 | - * @param array $parameters an array of parameters to be passed |
|
53 | - * @return mixed, the result of the method or false |
|
54 | - */ |
|
55 | - protected function walkBackends($gid, $method, $parameters) { |
|
56 | - $cacheKey = $this->getGroupCacheKey($gid); |
|
57 | - foreach($this->backends as $configPrefix => $backend) { |
|
58 | - if($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
59 | - $this->writeToCache($cacheKey, $configPrefix); |
|
60 | - return $result; |
|
61 | - } |
|
62 | - } |
|
63 | - return false; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Asks the backend connected to the server that supposely takes care of the gid from the request. |
|
68 | - * @param string $gid the gid connected to the request |
|
69 | - * @param string $method the method of the group backend that shall be called |
|
70 | - * @param array $parameters an array of parameters to be passed |
|
71 | - * @param mixed $passOnWhen the result matches this variable |
|
72 | - * @return mixed, the result of the method or false |
|
73 | - */ |
|
74 | - protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) { |
|
75 | - $cacheKey = $this->getGroupCacheKey($gid);; |
|
76 | - $prefix = $this->getFromCache($cacheKey); |
|
77 | - //in case the uid has been found in the past, try this stored connection first |
|
78 | - if(!is_null($prefix)) { |
|
79 | - if(isset($this->backends[$prefix])) { |
|
80 | - $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters); |
|
81 | - if($result === $passOnWhen) { |
|
82 | - //not found here, reset cache to null if group vanished |
|
83 | - //because sometimes methods return false with a reason |
|
84 | - $groupExists = call_user_func_array( |
|
85 | - array($this->backends[$prefix], 'groupExists'), |
|
86 | - array($gid) |
|
87 | - ); |
|
88 | - if(!$groupExists) { |
|
89 | - $this->writeToCache($cacheKey, null); |
|
90 | - } |
|
91 | - } |
|
92 | - return $result; |
|
93 | - } |
|
94 | - } |
|
95 | - return false; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * is user in group? |
|
100 | - * @param string $uid uid of the user |
|
101 | - * @param string $gid gid of the group |
|
102 | - * @return bool |
|
103 | - * |
|
104 | - * Checks whether the user is member of a group or not. |
|
105 | - */ |
|
106 | - public function inGroup($uid, $gid) { |
|
107 | - return $this->handleRequest($gid, 'inGroup', array($uid, $gid)); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get all groups a user belongs to |
|
112 | - * @param string $uid Name of the user |
|
113 | - * @return string[] with group names |
|
114 | - * |
|
115 | - * This function fetches all groups a user belongs to. It does not check |
|
116 | - * if the user exists at all. |
|
117 | - */ |
|
118 | - public function getUserGroups($uid) { |
|
119 | - $groups = array(); |
|
120 | - |
|
121 | - foreach($this->backends as $backend) { |
|
122 | - $backendGroups = $backend->getUserGroups($uid); |
|
123 | - if (is_array($backendGroups)) { |
|
124 | - $groups = array_merge($groups, $backendGroups); |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - return $groups; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * get a list of all users in a group |
|
133 | - * @return string[] with user ids |
|
134 | - */ |
|
135 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
136 | - $users = array(); |
|
137 | - |
|
138 | - foreach($this->backends as $backend) { |
|
139 | - $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset); |
|
140 | - if (is_array($backendUsers)) { |
|
141 | - $users = array_merge($users, $backendUsers); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - return $users; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * returns the number of users in a group, who match the search term |
|
150 | - * @param string $gid the internal group name |
|
151 | - * @param string $search optional, a search string |
|
152 | - * @return int|bool |
|
153 | - */ |
|
154 | - public function countUsersInGroup($gid, $search = '') { |
|
155 | - return $this->handleRequest( |
|
156 | - $gid, 'countUsersInGroup', array($gid, $search)); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * get a list of all groups |
|
161 | - * @return string[] with group names |
|
162 | - * |
|
163 | - * Returns a list with all groups |
|
164 | - */ |
|
165 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
166 | - $groups = array(); |
|
167 | - |
|
168 | - foreach($this->backends as $backend) { |
|
169 | - $backendGroups = $backend->getGroups($search, $limit, $offset); |
|
170 | - if (is_array($backendGroups)) { |
|
171 | - $groups = array_merge($groups, $backendGroups); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - return $groups; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * check if a group exists |
|
180 | - * @param string $gid |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - public function groupExists($gid) { |
|
184 | - return $this->handleRequest($gid, 'groupExists', array($gid)); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Check if backend implements actions |
|
189 | - * @param int $actions bitwise-or'ed actions |
|
190 | - * @return boolean |
|
191 | - * |
|
192 | - * Returns the supported actions as int to be |
|
193 | - * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
194 | - */ |
|
195 | - public function implementsActions($actions) { |
|
196 | - //it's the same across all our user backends obviously |
|
197 | - return $this->refBackend->implementsActions($actions); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Return access for LDAP interaction. |
|
202 | - * @param string $gid |
|
203 | - * @return Access instance of Access for LDAP interaction |
|
204 | - */ |
|
205 | - public function getLDAPAccess($gid) { |
|
206 | - return $this->handleRequest($gid, 'getLDAPAccess', []); |
|
207 | - } |
|
30 | + private $backends = array(); |
|
31 | + private $refBackend = null; |
|
32 | + |
|
33 | + /** |
|
34 | + * Constructor |
|
35 | + * @param string[] $serverConfigPrefixes array containing the config Prefixes |
|
36 | + */ |
|
37 | + public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) { |
|
38 | + parent::__construct($ldap); |
|
39 | + foreach($serverConfigPrefixes as $configPrefix) { |
|
40 | + $this->backends[$configPrefix] = |
|
41 | + new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix)); |
|
42 | + if(is_null($this->refBackend)) { |
|
43 | + $this->refBackend = &$this->backends[$configPrefix]; |
|
44 | + } |
|
45 | + } |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Tries the backends one after the other until a positive result is returned from the specified method |
|
50 | + * @param string $gid the gid connected to the request |
|
51 | + * @param string $method the method of the group backend that shall be called |
|
52 | + * @param array $parameters an array of parameters to be passed |
|
53 | + * @return mixed, the result of the method or false |
|
54 | + */ |
|
55 | + protected function walkBackends($gid, $method, $parameters) { |
|
56 | + $cacheKey = $this->getGroupCacheKey($gid); |
|
57 | + foreach($this->backends as $configPrefix => $backend) { |
|
58 | + if($result = call_user_func_array(array($backend, $method), $parameters)) { |
|
59 | + $this->writeToCache($cacheKey, $configPrefix); |
|
60 | + return $result; |
|
61 | + } |
|
62 | + } |
|
63 | + return false; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Asks the backend connected to the server that supposely takes care of the gid from the request. |
|
68 | + * @param string $gid the gid connected to the request |
|
69 | + * @param string $method the method of the group backend that shall be called |
|
70 | + * @param array $parameters an array of parameters to be passed |
|
71 | + * @param mixed $passOnWhen the result matches this variable |
|
72 | + * @return mixed, the result of the method or false |
|
73 | + */ |
|
74 | + protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) { |
|
75 | + $cacheKey = $this->getGroupCacheKey($gid);; |
|
76 | + $prefix = $this->getFromCache($cacheKey); |
|
77 | + //in case the uid has been found in the past, try this stored connection first |
|
78 | + if(!is_null($prefix)) { |
|
79 | + if(isset($this->backends[$prefix])) { |
|
80 | + $result = call_user_func_array(array($this->backends[$prefix], $method), $parameters); |
|
81 | + if($result === $passOnWhen) { |
|
82 | + //not found here, reset cache to null if group vanished |
|
83 | + //because sometimes methods return false with a reason |
|
84 | + $groupExists = call_user_func_array( |
|
85 | + array($this->backends[$prefix], 'groupExists'), |
|
86 | + array($gid) |
|
87 | + ); |
|
88 | + if(!$groupExists) { |
|
89 | + $this->writeToCache($cacheKey, null); |
|
90 | + } |
|
91 | + } |
|
92 | + return $result; |
|
93 | + } |
|
94 | + } |
|
95 | + return false; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * is user in group? |
|
100 | + * @param string $uid uid of the user |
|
101 | + * @param string $gid gid of the group |
|
102 | + * @return bool |
|
103 | + * |
|
104 | + * Checks whether the user is member of a group or not. |
|
105 | + */ |
|
106 | + public function inGroup($uid, $gid) { |
|
107 | + return $this->handleRequest($gid, 'inGroup', array($uid, $gid)); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get all groups a user belongs to |
|
112 | + * @param string $uid Name of the user |
|
113 | + * @return string[] with group names |
|
114 | + * |
|
115 | + * This function fetches all groups a user belongs to. It does not check |
|
116 | + * if the user exists at all. |
|
117 | + */ |
|
118 | + public function getUserGroups($uid) { |
|
119 | + $groups = array(); |
|
120 | + |
|
121 | + foreach($this->backends as $backend) { |
|
122 | + $backendGroups = $backend->getUserGroups($uid); |
|
123 | + if (is_array($backendGroups)) { |
|
124 | + $groups = array_merge($groups, $backendGroups); |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + return $groups; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * get a list of all users in a group |
|
133 | + * @return string[] with user ids |
|
134 | + */ |
|
135 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
136 | + $users = array(); |
|
137 | + |
|
138 | + foreach($this->backends as $backend) { |
|
139 | + $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset); |
|
140 | + if (is_array($backendUsers)) { |
|
141 | + $users = array_merge($users, $backendUsers); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + return $users; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * returns the number of users in a group, who match the search term |
|
150 | + * @param string $gid the internal group name |
|
151 | + * @param string $search optional, a search string |
|
152 | + * @return int|bool |
|
153 | + */ |
|
154 | + public function countUsersInGroup($gid, $search = '') { |
|
155 | + return $this->handleRequest( |
|
156 | + $gid, 'countUsersInGroup', array($gid, $search)); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * get a list of all groups |
|
161 | + * @return string[] with group names |
|
162 | + * |
|
163 | + * Returns a list with all groups |
|
164 | + */ |
|
165 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
166 | + $groups = array(); |
|
167 | + |
|
168 | + foreach($this->backends as $backend) { |
|
169 | + $backendGroups = $backend->getGroups($search, $limit, $offset); |
|
170 | + if (is_array($backendGroups)) { |
|
171 | + $groups = array_merge($groups, $backendGroups); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + return $groups; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * check if a group exists |
|
180 | + * @param string $gid |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + public function groupExists($gid) { |
|
184 | + return $this->handleRequest($gid, 'groupExists', array($gid)); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Check if backend implements actions |
|
189 | + * @param int $actions bitwise-or'ed actions |
|
190 | + * @return boolean |
|
191 | + * |
|
192 | + * Returns the supported actions as int to be |
|
193 | + * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
194 | + */ |
|
195 | + public function implementsActions($actions) { |
|
196 | + //it's the same across all our user backends obviously |
|
197 | + return $this->refBackend->implementsActions($actions); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Return access for LDAP interaction. |
|
202 | + * @param string $gid |
|
203 | + * @return Access instance of Access for LDAP interaction |
|
204 | + */ |
|
205 | + public function getLDAPAccess($gid) { |
|
206 | + return $this->handleRequest($gid, 'getLDAPAccess', []); |
|
207 | + } |
|
208 | 208 | } |
@@ -37,1324 +37,1324 @@ |
||
37 | 37 | use OC\ServerNotAvailableException; |
38 | 38 | |
39 | 39 | class Wizard extends LDAPUtility { |
40 | - /** @var \OCP\IL10N */ |
|
41 | - static protected $l; |
|
42 | - protected $access; |
|
43 | - protected $cr; |
|
44 | - protected $configuration; |
|
45 | - protected $result; |
|
46 | - protected $resultCache = array(); |
|
47 | - |
|
48 | - const LRESULT_PROCESSED_OK = 2; |
|
49 | - const LRESULT_PROCESSED_INVALID = 3; |
|
50 | - const LRESULT_PROCESSED_SKIP = 4; |
|
51 | - |
|
52 | - const LFILTER_LOGIN = 2; |
|
53 | - const LFILTER_USER_LIST = 3; |
|
54 | - const LFILTER_GROUP_LIST = 4; |
|
55 | - |
|
56 | - const LFILTER_MODE_ASSISTED = 2; |
|
57 | - const LFILTER_MODE_RAW = 1; |
|
58 | - |
|
59 | - const LDAP_NW_TIMEOUT = 4; |
|
60 | - |
|
61 | - /** |
|
62 | - * Constructor |
|
63 | - * @param Configuration $configuration an instance of Configuration |
|
64 | - * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
65 | - * @param Access $access |
|
66 | - */ |
|
67 | - public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
68 | - parent::__construct($ldap); |
|
69 | - $this->configuration = $configuration; |
|
70 | - if(is_null(Wizard::$l)) { |
|
71 | - Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
72 | - } |
|
73 | - $this->access = $access; |
|
74 | - $this->result = new WizardResult(); |
|
75 | - } |
|
76 | - |
|
77 | - public function __destruct() { |
|
78 | - if($this->result->hasChanges()) { |
|
79 | - $this->configuration->saveConfiguration(); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * counts entries in the LDAP directory |
|
85 | - * |
|
86 | - * @param string $filter the LDAP search filter |
|
87 | - * @param string $type a string being either 'users' or 'groups'; |
|
88 | - * @return bool|int |
|
89 | - * @throws \Exception |
|
90 | - */ |
|
91 | - public function countEntries($filter, $type) { |
|
92 | - $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
93 | - if($type === 'users') { |
|
94 | - $reqs[] = 'ldapUserFilter'; |
|
95 | - } |
|
96 | - if(!$this->checkRequirements($reqs)) { |
|
97 | - throw new \Exception('Requirements not met', 400); |
|
98 | - } |
|
99 | - |
|
100 | - $attr = array('dn'); // default |
|
101 | - $limit = 1001; |
|
102 | - if($type === 'groups') { |
|
103 | - $result = $this->access->countGroups($filter, $attr, $limit); |
|
104 | - } else if($type === 'users') { |
|
105 | - $result = $this->access->countUsers($filter, $attr, $limit); |
|
106 | - } else if ($type === 'objects') { |
|
107 | - $result = $this->access->countObjects($limit); |
|
108 | - } else { |
|
109 | - throw new \Exception('internal error: invalid object type', 500); |
|
110 | - } |
|
111 | - |
|
112 | - return $result; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * formats the return value of a count operation to the string to be |
|
117 | - * inserted. |
|
118 | - * |
|
119 | - * @param bool|int $count |
|
120 | - * @return int|string |
|
121 | - */ |
|
122 | - private function formatCountResult($count) { |
|
123 | - $formatted = ($count !== false) ? $count : 0; |
|
124 | - if($formatted > 1000) { |
|
125 | - $formatted = '> 1000'; |
|
126 | - } |
|
127 | - return $formatted; |
|
128 | - } |
|
129 | - |
|
130 | - public function countGroups() { |
|
131 | - $filter = $this->configuration->ldapGroupFilter; |
|
132 | - |
|
133 | - if(empty($filter)) { |
|
134 | - $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
135 | - $this->result->addChange('ldap_group_count', $output); |
|
136 | - return $this->result; |
|
137 | - } |
|
138 | - |
|
139 | - try { |
|
140 | - $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
141 | - } catch (\Exception $e) { |
|
142 | - //400 can be ignored, 500 is forwarded |
|
143 | - if($e->getCode() === 500) { |
|
144 | - throw $e; |
|
145 | - } |
|
146 | - return false; |
|
147 | - } |
|
148 | - $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
149 | - $this->result->addChange('ldap_group_count', $output); |
|
150 | - return $this->result; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return WizardResult |
|
155 | - * @throws \Exception |
|
156 | - */ |
|
157 | - public function countUsers() { |
|
158 | - $filter = $this->access->getFilterForUserCount(); |
|
159 | - |
|
160 | - $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
161 | - $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
162 | - $this->result->addChange('ldap_user_count', $output); |
|
163 | - return $this->result; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * counts any objects in the currently set base dn |
|
168 | - * |
|
169 | - * @return WizardResult |
|
170 | - * @throws \Exception |
|
171 | - */ |
|
172 | - public function countInBaseDN() { |
|
173 | - // we don't need to provide a filter in this case |
|
174 | - $total = $this->countEntries(null, 'objects'); |
|
175 | - if($total === false) { |
|
176 | - throw new \Exception('invalid results received'); |
|
177 | - } |
|
178 | - $this->result->addChange('ldap_test_base', $total); |
|
179 | - return $this->result; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * counts users with a specified attribute |
|
184 | - * @param string $attr |
|
185 | - * @param bool $existsCheck |
|
186 | - * @return int|bool |
|
187 | - */ |
|
188 | - public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
189 | - if(!$this->checkRequirements(array('ldapHost', |
|
190 | - 'ldapPort', |
|
191 | - 'ldapBase', |
|
192 | - 'ldapUserFilter', |
|
193 | - ))) { |
|
194 | - return false; |
|
195 | - } |
|
196 | - |
|
197 | - $filter = $this->access->combineFilterWithAnd(array( |
|
198 | - $this->configuration->ldapUserFilter, |
|
199 | - $attr . '=*' |
|
200 | - )); |
|
201 | - |
|
202 | - $limit = ($existsCheck === false) ? null : 1; |
|
203 | - |
|
204 | - return $this->access->countUsers($filter, array('dn'), $limit); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * detects the display name attribute. If a setting is already present that |
|
209 | - * returns at least one hit, the detection will be canceled. |
|
210 | - * @return WizardResult|bool |
|
211 | - * @throws \Exception |
|
212 | - */ |
|
213 | - public function detectUserDisplayNameAttribute() { |
|
214 | - if(!$this->checkRequirements(array('ldapHost', |
|
215 | - 'ldapPort', |
|
216 | - 'ldapBase', |
|
217 | - 'ldapUserFilter', |
|
218 | - ))) { |
|
219 | - return false; |
|
220 | - } |
|
221 | - |
|
222 | - $attr = $this->configuration->ldapUserDisplayName; |
|
223 | - if ($attr !== '' && $attr !== 'displayName') { |
|
224 | - // most likely not the default value with upper case N, |
|
225 | - // verify it still produces a result |
|
226 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
227 | - if($count > 0) { |
|
228 | - //no change, but we sent it back to make sure the user interface |
|
229 | - //is still correct, even if the ajax call was cancelled meanwhile |
|
230 | - $this->result->addChange('ldap_display_name', $attr); |
|
231 | - return $this->result; |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - // first attribute that has at least one result wins |
|
236 | - $displayNameAttrs = array('displayname', 'cn'); |
|
237 | - foreach ($displayNameAttrs as $attr) { |
|
238 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
239 | - |
|
240 | - if($count > 0) { |
|
241 | - $this->applyFind('ldap_display_name', $attr); |
|
242 | - return $this->result; |
|
243 | - } |
|
244 | - }; |
|
245 | - |
|
246 | - throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.')); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * detects the most often used email attribute for users applying to the |
|
251 | - * user list filter. If a setting is already present that returns at least |
|
252 | - * one hit, the detection will be canceled. |
|
253 | - * @return WizardResult|bool |
|
254 | - */ |
|
255 | - public function detectEmailAttribute() { |
|
256 | - if(!$this->checkRequirements(array('ldapHost', |
|
257 | - 'ldapPort', |
|
258 | - 'ldapBase', |
|
259 | - 'ldapUserFilter', |
|
260 | - ))) { |
|
261 | - return false; |
|
262 | - } |
|
263 | - |
|
264 | - $attr = $this->configuration->ldapEmailAttribute; |
|
265 | - if ($attr !== '') { |
|
266 | - $count = intval($this->countUsersWithAttribute($attr, true)); |
|
267 | - if($count > 0) { |
|
268 | - return false; |
|
269 | - } |
|
270 | - $writeLog = true; |
|
271 | - } else { |
|
272 | - $writeLog = false; |
|
273 | - } |
|
274 | - |
|
275 | - $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
276 | - $winner = ''; |
|
277 | - $maxUsers = 0; |
|
278 | - foreach($emailAttributes as $attr) { |
|
279 | - $count = $this->countUsersWithAttribute($attr); |
|
280 | - if($count > $maxUsers) { |
|
281 | - $maxUsers = $count; |
|
282 | - $winner = $attr; |
|
283 | - } |
|
284 | - } |
|
285 | - |
|
286 | - if($winner !== '') { |
|
287 | - $this->applyFind('ldap_email_attr', $winner); |
|
288 | - if($writeLog) { |
|
289 | - \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
290 | - 'automatically been reset, because the original value ' . |
|
291 | - 'did not return any results.', \OCP\Util::INFO); |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - return $this->result; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * @return WizardResult |
|
300 | - * @throws \Exception |
|
301 | - */ |
|
302 | - public function determineAttributes() { |
|
303 | - if(!$this->checkRequirements(array('ldapHost', |
|
304 | - 'ldapPort', |
|
305 | - 'ldapBase', |
|
306 | - 'ldapUserFilter', |
|
307 | - ))) { |
|
308 | - return false; |
|
309 | - } |
|
310 | - |
|
311 | - $attributes = $this->getUserAttributes(); |
|
312 | - |
|
313 | - natcasesort($attributes); |
|
314 | - $attributes = array_values($attributes); |
|
315 | - |
|
316 | - $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
317 | - |
|
318 | - $selected = $this->configuration->ldapLoginFilterAttributes; |
|
319 | - if(is_array($selected) && !empty($selected)) { |
|
320 | - $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
321 | - } |
|
322 | - |
|
323 | - return $this->result; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * detects the available LDAP attributes |
|
328 | - * @return array|false The instance's WizardResult instance |
|
329 | - * @throws \Exception |
|
330 | - */ |
|
331 | - private function getUserAttributes() { |
|
332 | - if(!$this->checkRequirements(array('ldapHost', |
|
333 | - 'ldapPort', |
|
334 | - 'ldapBase', |
|
335 | - 'ldapUserFilter', |
|
336 | - ))) { |
|
337 | - return false; |
|
338 | - } |
|
339 | - $cr = $this->getConnection(); |
|
340 | - if(!$cr) { |
|
341 | - throw new \Exception('Could not connect to LDAP'); |
|
342 | - } |
|
343 | - |
|
344 | - $base = $this->configuration->ldapBase[0]; |
|
345 | - $filter = $this->configuration->ldapUserFilter; |
|
346 | - $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
347 | - if(!$this->ldap->isResource($rr)) { |
|
348 | - return false; |
|
349 | - } |
|
350 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
351 | - $attributes = $this->ldap->getAttributes($cr, $er); |
|
352 | - $pureAttributes = array(); |
|
353 | - for($i = 0; $i < $attributes['count']; $i++) { |
|
354 | - $pureAttributes[] = $attributes[$i]; |
|
355 | - } |
|
356 | - |
|
357 | - return $pureAttributes; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * detects the available LDAP groups |
|
362 | - * @return WizardResult|false the instance's WizardResult instance |
|
363 | - */ |
|
364 | - public function determineGroupsForGroups() { |
|
365 | - return $this->determineGroups('ldap_groupfilter_groups', |
|
366 | - 'ldapGroupFilterGroups', |
|
367 | - false); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * detects the available LDAP groups |
|
372 | - * @return WizardResult|false the instance's WizardResult instance |
|
373 | - */ |
|
374 | - public function determineGroupsForUsers() { |
|
375 | - return $this->determineGroups('ldap_userfilter_groups', |
|
376 | - 'ldapUserFilterGroups'); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * detects the available LDAP groups |
|
381 | - * @param string $dbKey |
|
382 | - * @param string $confKey |
|
383 | - * @param bool $testMemberOf |
|
384 | - * @return WizardResult|false the instance's WizardResult instance |
|
385 | - * @throws \Exception |
|
386 | - */ |
|
387 | - private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
388 | - if(!$this->checkRequirements(array('ldapHost', |
|
389 | - 'ldapPort', |
|
390 | - 'ldapBase', |
|
391 | - ))) { |
|
392 | - return false; |
|
393 | - } |
|
394 | - $cr = $this->getConnection(); |
|
395 | - if(!$cr) { |
|
396 | - throw new \Exception('Could not connect to LDAP'); |
|
397 | - } |
|
398 | - |
|
399 | - $this->fetchGroups($dbKey, $confKey); |
|
400 | - |
|
401 | - if($testMemberOf) { |
|
402 | - $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
403 | - $this->result->markChange(); |
|
404 | - if(!$this->configuration->hasMemberOfFilterSupport) { |
|
405 | - throw new \Exception('memberOf is not supported by the server'); |
|
406 | - } |
|
407 | - } |
|
408 | - |
|
409 | - return $this->result; |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * fetches all groups from LDAP and adds them to the result object |
|
414 | - * |
|
415 | - * @param string $dbKey |
|
416 | - * @param string $confKey |
|
417 | - * @return array $groupEntries |
|
418 | - * @throws \Exception |
|
419 | - */ |
|
420 | - public function fetchGroups($dbKey, $confKey) { |
|
421 | - $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
|
422 | - |
|
423 | - $filterParts = array(); |
|
424 | - foreach($obclasses as $obclass) { |
|
425 | - $filterParts[] = 'objectclass='.$obclass; |
|
426 | - } |
|
427 | - //we filter for everything |
|
428 | - //- that looks like a group and |
|
429 | - //- has the group display name set |
|
430 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
431 | - $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
432 | - |
|
433 | - $groupNames = array(); |
|
434 | - $groupEntries = array(); |
|
435 | - $limit = 400; |
|
436 | - $offset = 0; |
|
437 | - do { |
|
438 | - // we need to request dn additionally here, otherwise memberOf |
|
439 | - // detection will fail later |
|
440 | - $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
441 | - foreach($result as $item) { |
|
442 | - if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
443 | - // just in case - no issue known |
|
444 | - continue; |
|
445 | - } |
|
446 | - $groupNames[] = $item['cn'][0]; |
|
447 | - $groupEntries[] = $item; |
|
448 | - } |
|
449 | - $offset += $limit; |
|
450 | - } while ($this->access->hasMoreResults()); |
|
451 | - |
|
452 | - if(count($groupNames) > 0) { |
|
453 | - natsort($groupNames); |
|
454 | - $this->result->addOptions($dbKey, array_values($groupNames)); |
|
455 | - } else { |
|
456 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
457 | - } |
|
458 | - |
|
459 | - $setFeatures = $this->configuration->$confKey; |
|
460 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
461 | - //something is already configured? pre-select it. |
|
462 | - $this->result->addChange($dbKey, $setFeatures); |
|
463 | - } |
|
464 | - return $groupEntries; |
|
465 | - } |
|
466 | - |
|
467 | - public function determineGroupMemberAssoc() { |
|
468 | - if(!$this->checkRequirements(array('ldapHost', |
|
469 | - 'ldapPort', |
|
470 | - 'ldapGroupFilter', |
|
471 | - ))) { |
|
472 | - return false; |
|
473 | - } |
|
474 | - $attribute = $this->detectGroupMemberAssoc(); |
|
475 | - if($attribute === false) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
479 | - $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
480 | - |
|
481 | - return $this->result; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Detects the available object classes |
|
486 | - * @return WizardResult|false the instance's WizardResult instance |
|
487 | - * @throws \Exception |
|
488 | - */ |
|
489 | - public function determineGroupObjectClasses() { |
|
490 | - if(!$this->checkRequirements(array('ldapHost', |
|
491 | - 'ldapPort', |
|
492 | - 'ldapBase', |
|
493 | - ))) { |
|
494 | - return false; |
|
495 | - } |
|
496 | - $cr = $this->getConnection(); |
|
497 | - if(!$cr) { |
|
498 | - throw new \Exception('Could not connect to LDAP'); |
|
499 | - } |
|
500 | - |
|
501 | - $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
502 | - $this->determineFeature($obclasses, |
|
503 | - 'objectclass', |
|
504 | - 'ldap_groupfilter_objectclass', |
|
505 | - 'ldapGroupFilterObjectclass', |
|
506 | - false); |
|
507 | - |
|
508 | - return $this->result; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * detects the available object classes |
|
513 | - * @return WizardResult |
|
514 | - * @throws \Exception |
|
515 | - */ |
|
516 | - public function determineUserObjectClasses() { |
|
517 | - if(!$this->checkRequirements(array('ldapHost', |
|
518 | - 'ldapPort', |
|
519 | - 'ldapBase', |
|
520 | - ))) { |
|
521 | - return false; |
|
522 | - } |
|
523 | - $cr = $this->getConnection(); |
|
524 | - if(!$cr) { |
|
525 | - throw new \Exception('Could not connect to LDAP'); |
|
526 | - } |
|
527 | - |
|
528 | - $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
529 | - 'user', 'posixAccount', '*'); |
|
530 | - $filter = $this->configuration->ldapUserFilter; |
|
531 | - //if filter is empty, it is probably the first time the wizard is called |
|
532 | - //then, apply suggestions. |
|
533 | - $this->determineFeature($obclasses, |
|
534 | - 'objectclass', |
|
535 | - 'ldap_userfilter_objectclass', |
|
536 | - 'ldapUserFilterObjectclass', |
|
537 | - empty($filter)); |
|
538 | - |
|
539 | - return $this->result; |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * @return WizardResult|false |
|
544 | - * @throws \Exception |
|
545 | - */ |
|
546 | - public function getGroupFilter() { |
|
547 | - if(!$this->checkRequirements(array('ldapHost', |
|
548 | - 'ldapPort', |
|
549 | - 'ldapBase', |
|
550 | - ))) { |
|
551 | - return false; |
|
552 | - } |
|
553 | - //make sure the use display name is set |
|
554 | - $displayName = $this->configuration->ldapGroupDisplayName; |
|
555 | - if ($displayName === '') { |
|
556 | - $d = $this->configuration->getDefaults(); |
|
557 | - $this->applyFind('ldap_group_display_name', |
|
558 | - $d['ldap_group_display_name']); |
|
559 | - } |
|
560 | - $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
561 | - |
|
562 | - $this->applyFind('ldap_group_filter', $filter); |
|
563 | - return $this->result; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * @return WizardResult|false |
|
568 | - * @throws \Exception |
|
569 | - */ |
|
570 | - public function getUserListFilter() { |
|
571 | - if(!$this->checkRequirements(array('ldapHost', |
|
572 | - 'ldapPort', |
|
573 | - 'ldapBase', |
|
574 | - ))) { |
|
575 | - return false; |
|
576 | - } |
|
577 | - //make sure the use display name is set |
|
578 | - $displayName = $this->configuration->ldapUserDisplayName; |
|
579 | - if ($displayName === '') { |
|
580 | - $d = $this->configuration->getDefaults(); |
|
581 | - $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
582 | - } |
|
583 | - $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
584 | - if(!$filter) { |
|
585 | - throw new \Exception('Cannot create filter'); |
|
586 | - } |
|
587 | - |
|
588 | - $this->applyFind('ldap_userlist_filter', $filter); |
|
589 | - return $this->result; |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * @return bool|WizardResult |
|
594 | - * @throws \Exception |
|
595 | - */ |
|
596 | - public function getUserLoginFilter() { |
|
597 | - if(!$this->checkRequirements(array('ldapHost', |
|
598 | - 'ldapPort', |
|
599 | - 'ldapBase', |
|
600 | - 'ldapUserFilter', |
|
601 | - ))) { |
|
602 | - return false; |
|
603 | - } |
|
604 | - |
|
605 | - $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
606 | - if(!$filter) { |
|
607 | - throw new \Exception('Cannot create filter'); |
|
608 | - } |
|
609 | - |
|
610 | - $this->applyFind('ldap_login_filter', $filter); |
|
611 | - return $this->result; |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * @return bool|WizardResult |
|
616 | - * @param string $loginName |
|
617 | - * @throws \Exception |
|
618 | - */ |
|
619 | - public function testLoginName($loginName) { |
|
620 | - if(!$this->checkRequirements(array('ldapHost', |
|
621 | - 'ldapPort', |
|
622 | - 'ldapBase', |
|
623 | - 'ldapLoginFilter', |
|
624 | - ))) { |
|
625 | - return false; |
|
626 | - } |
|
627 | - |
|
628 | - $cr = $this->access->connection->getConnectionResource(); |
|
629 | - if(!$this->ldap->isResource($cr)) { |
|
630 | - throw new \Exception('connection error'); |
|
631 | - } |
|
632 | - |
|
633 | - if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
634 | - === false) { |
|
635 | - throw new \Exception('missing placeholder'); |
|
636 | - } |
|
637 | - |
|
638 | - $users = $this->access->countUsersByLoginName($loginName); |
|
639 | - if($this->ldap->errno($cr) !== 0) { |
|
640 | - throw new \Exception($this->ldap->error($cr)); |
|
641 | - } |
|
642 | - $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
643 | - $this->result->addChange('ldap_test_loginname', $users); |
|
644 | - $this->result->addChange('ldap_test_effective_filter', $filter); |
|
645 | - return $this->result; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Tries to determine the port, requires given Host, User DN and Password |
|
650 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
651 | - * @throws \Exception |
|
652 | - */ |
|
653 | - public function guessPortAndTLS() { |
|
654 | - if(!$this->checkRequirements(array('ldapHost', |
|
655 | - ))) { |
|
656 | - return false; |
|
657 | - } |
|
658 | - $this->checkHost(); |
|
659 | - $portSettings = $this->getPortSettingsToTry(); |
|
660 | - |
|
661 | - if(!is_array($portSettings)) { |
|
662 | - throw new \Exception(print_r($portSettings, true)); |
|
663 | - } |
|
664 | - |
|
665 | - //proceed from the best configuration and return on first success |
|
666 | - foreach($portSettings as $setting) { |
|
667 | - $p = $setting['port']; |
|
668 | - $t = $setting['tls']; |
|
669 | - \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
670 | - //connectAndBind may throw Exception, it needs to be catched by the |
|
671 | - //callee of this method |
|
672 | - |
|
673 | - try { |
|
674 | - $settingsFound = $this->connectAndBind($p, $t); |
|
675 | - } catch (\Exception $e) { |
|
676 | - // any reply other than -1 (= cannot connect) is already okay, |
|
677 | - // because then we found the server |
|
678 | - // unavailable startTLS returns -11 |
|
679 | - if($e->getCode() > 0) { |
|
680 | - $settingsFound = true; |
|
681 | - } else { |
|
682 | - throw $e; |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - if ($settingsFound === true) { |
|
687 | - $config = array( |
|
688 | - 'ldapPort' => $p, |
|
689 | - 'ldapTLS' => intval($t) |
|
690 | - ); |
|
691 | - $this->configuration->setConfiguration($config); |
|
692 | - \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
693 | - $this->result->addChange('ldap_port', $p); |
|
694 | - return $this->result; |
|
695 | - } |
|
696 | - } |
|
697 | - |
|
698 | - //custom port, undetected (we do not brute force) |
|
699 | - return false; |
|
700 | - } |
|
701 | - |
|
702 | - /** |
|
703 | - * tries to determine a base dn from User DN or LDAP Host |
|
704 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
705 | - */ |
|
706 | - public function guessBaseDN() { |
|
707 | - if(!$this->checkRequirements(array('ldapHost', |
|
708 | - 'ldapPort', |
|
709 | - ))) { |
|
710 | - return false; |
|
711 | - } |
|
712 | - |
|
713 | - //check whether a DN is given in the agent name (99.9% of all cases) |
|
714 | - $base = null; |
|
715 | - $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
716 | - if($i !== false) { |
|
717 | - $base = substr($this->configuration->ldapAgentName, $i); |
|
718 | - if($this->testBaseDN($base)) { |
|
719 | - $this->applyFind('ldap_base', $base); |
|
720 | - return $this->result; |
|
721 | - } |
|
722 | - } |
|
723 | - |
|
724 | - //this did not help :( |
|
725 | - //Let's see whether we can parse the Host URL and convert the domain to |
|
726 | - //a base DN |
|
727 | - $helper = new Helper(\OC::$server->getConfig()); |
|
728 | - $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
729 | - if(!$domain) { |
|
730 | - return false; |
|
731 | - } |
|
732 | - |
|
733 | - $dparts = explode('.', $domain); |
|
734 | - while(count($dparts) > 0) { |
|
735 | - $base2 = 'dc=' . implode(',dc=', $dparts); |
|
736 | - if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
737 | - $this->applyFind('ldap_base', $base2); |
|
738 | - return $this->result; |
|
739 | - } |
|
740 | - array_shift($dparts); |
|
741 | - } |
|
742 | - |
|
743 | - return false; |
|
744 | - } |
|
745 | - |
|
746 | - /** |
|
747 | - * sets the found value for the configuration key in the WizardResult |
|
748 | - * as well as in the Configuration instance |
|
749 | - * @param string $key the configuration key |
|
750 | - * @param string $value the (detected) value |
|
751 | - * |
|
752 | - */ |
|
753 | - private function applyFind($key, $value) { |
|
754 | - $this->result->addChange($key, $value); |
|
755 | - $this->configuration->setConfiguration(array($key => $value)); |
|
756 | - } |
|
757 | - |
|
758 | - /** |
|
759 | - * Checks, whether a port was entered in the Host configuration |
|
760 | - * field. In this case the port will be stripped off, but also stored as |
|
761 | - * setting. |
|
762 | - */ |
|
763 | - private function checkHost() { |
|
764 | - $host = $this->configuration->ldapHost; |
|
765 | - $hostInfo = parse_url($host); |
|
766 | - |
|
767 | - //removes Port from Host |
|
768 | - if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
769 | - $port = $hostInfo['port']; |
|
770 | - $host = str_replace(':'.$port, '', $host); |
|
771 | - $this->applyFind('ldap_host', $host); |
|
772 | - $this->applyFind('ldap_port', $port); |
|
773 | - } |
|
774 | - } |
|
775 | - |
|
776 | - /** |
|
777 | - * tries to detect the group member association attribute which is |
|
778 | - * one of 'uniqueMember', 'memberUid', 'member' |
|
779 | - * @return string|false, string with the attribute name, false on error |
|
780 | - * @throws \Exception |
|
781 | - */ |
|
782 | - private function detectGroupMemberAssoc() { |
|
783 | - $possibleAttrs = array('uniqueMember', 'memberUid', 'member'); |
|
784 | - $filter = $this->configuration->ldapGroupFilter; |
|
785 | - if(empty($filter)) { |
|
786 | - return false; |
|
787 | - } |
|
788 | - $cr = $this->getConnection(); |
|
789 | - if(!$cr) { |
|
790 | - throw new \Exception('Could not connect to LDAP'); |
|
791 | - } |
|
792 | - $base = $this->configuration->ldapBase[0]; |
|
793 | - $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
794 | - if(!$this->ldap->isResource($rr)) { |
|
795 | - return false; |
|
796 | - } |
|
797 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
798 | - while(is_resource($er)) { |
|
799 | - $this->ldap->getDN($cr, $er); |
|
800 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
801 | - $result = array(); |
|
802 | - $possibleAttrsCount = count($possibleAttrs); |
|
803 | - for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
804 | - if(isset($attrs[$possibleAttrs[$i]])) { |
|
805 | - $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
806 | - } |
|
807 | - } |
|
808 | - if(!empty($result)) { |
|
809 | - natsort($result); |
|
810 | - return key($result); |
|
811 | - } |
|
812 | - |
|
813 | - $er = $this->ldap->nextEntry($cr, $er); |
|
814 | - } |
|
815 | - |
|
816 | - return false; |
|
817 | - } |
|
818 | - |
|
819 | - /** |
|
820 | - * Checks whether for a given BaseDN results will be returned |
|
821 | - * @param string $base the BaseDN to test |
|
822 | - * @return bool true on success, false otherwise |
|
823 | - * @throws \Exception |
|
824 | - */ |
|
825 | - private function testBaseDN($base) { |
|
826 | - $cr = $this->getConnection(); |
|
827 | - if(!$cr) { |
|
828 | - throw new \Exception('Could not connect to LDAP'); |
|
829 | - } |
|
830 | - |
|
831 | - //base is there, let's validate it. If we search for anything, we should |
|
832 | - //get a result set > 0 on a proper base |
|
833 | - $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
834 | - if(!$this->ldap->isResource($rr)) { |
|
835 | - $errorNo = $this->ldap->errno($cr); |
|
836 | - $errorMsg = $this->ldap->error($cr); |
|
837 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
838 | - ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
839 | - return false; |
|
840 | - } |
|
841 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
842 | - return ($entries !== false) && ($entries > 0); |
|
843 | - } |
|
844 | - |
|
845 | - /** |
|
846 | - * Checks whether the server supports memberOf in LDAP Filter. |
|
847 | - * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
848 | - * a configured objectClass. I.e. not necessarily for all available groups |
|
849 | - * memberOf does work. |
|
850 | - * |
|
851 | - * @return bool true if it does, false otherwise |
|
852 | - * @throws \Exception |
|
853 | - */ |
|
854 | - private function testMemberOf() { |
|
855 | - $cr = $this->getConnection(); |
|
856 | - if(!$cr) { |
|
857 | - throw new \Exception('Could not connect to LDAP'); |
|
858 | - } |
|
859 | - $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
860 | - if(is_int($result) && $result > 0) { |
|
861 | - return true; |
|
862 | - } |
|
863 | - return false; |
|
864 | - } |
|
865 | - |
|
866 | - /** |
|
867 | - * creates an LDAP Filter from given configuration |
|
868 | - * @param integer $filterType int, for which use case the filter shall be created |
|
869 | - * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
870 | - * self::LFILTER_GROUP_LIST |
|
871 | - * @return string|false string with the filter on success, false otherwise |
|
872 | - * @throws \Exception |
|
873 | - */ |
|
874 | - private function composeLdapFilter($filterType) { |
|
875 | - $filter = ''; |
|
876 | - $parts = 0; |
|
877 | - switch ($filterType) { |
|
878 | - case self::LFILTER_USER_LIST: |
|
879 | - $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
880 | - //glue objectclasses |
|
881 | - if(is_array($objcs) && count($objcs) > 0) { |
|
882 | - $filter .= '(|'; |
|
883 | - foreach($objcs as $objc) { |
|
884 | - $filter .= '(objectclass=' . $objc . ')'; |
|
885 | - } |
|
886 | - $filter .= ')'; |
|
887 | - $parts++; |
|
888 | - } |
|
889 | - //glue group memberships |
|
890 | - if($this->configuration->hasMemberOfFilterSupport) { |
|
891 | - $cns = $this->configuration->ldapUserFilterGroups; |
|
892 | - if(is_array($cns) && count($cns) > 0) { |
|
893 | - $filter .= '(|'; |
|
894 | - $cr = $this->getConnection(); |
|
895 | - if(!$cr) { |
|
896 | - throw new \Exception('Could not connect to LDAP'); |
|
897 | - } |
|
898 | - $base = $this->configuration->ldapBase[0]; |
|
899 | - foreach($cns as $cn) { |
|
900 | - $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
901 | - if(!$this->ldap->isResource($rr)) { |
|
902 | - continue; |
|
903 | - } |
|
904 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
905 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
906 | - $dn = $this->ldap->getDN($cr, $er); |
|
907 | - if ($dn == false || $dn === '') { |
|
908 | - continue; |
|
909 | - } |
|
910 | - $filterPart = '(memberof=' . $dn . ')'; |
|
911 | - if(isset($attrs['primaryGroupToken'])) { |
|
912 | - $pgt = $attrs['primaryGroupToken'][0]; |
|
913 | - $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
914 | - $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
915 | - } |
|
916 | - $filter .= $filterPart; |
|
917 | - } |
|
918 | - $filter .= ')'; |
|
919 | - } |
|
920 | - $parts++; |
|
921 | - } |
|
922 | - //wrap parts in AND condition |
|
923 | - if($parts > 1) { |
|
924 | - $filter = '(&' . $filter . ')'; |
|
925 | - } |
|
926 | - if ($filter === '') { |
|
927 | - $filter = '(objectclass=*)'; |
|
928 | - } |
|
929 | - break; |
|
930 | - |
|
931 | - case self::LFILTER_GROUP_LIST: |
|
932 | - $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
933 | - //glue objectclasses |
|
934 | - if(is_array($objcs) && count($objcs) > 0) { |
|
935 | - $filter .= '(|'; |
|
936 | - foreach($objcs as $objc) { |
|
937 | - $filter .= '(objectclass=' . $objc . ')'; |
|
938 | - } |
|
939 | - $filter .= ')'; |
|
940 | - $parts++; |
|
941 | - } |
|
942 | - //glue group memberships |
|
943 | - $cns = $this->configuration->ldapGroupFilterGroups; |
|
944 | - if(is_array($cns) && count($cns) > 0) { |
|
945 | - $filter .= '(|'; |
|
946 | - foreach($cns as $cn) { |
|
947 | - $filter .= '(cn=' . $cn . ')'; |
|
948 | - } |
|
949 | - $filter .= ')'; |
|
950 | - } |
|
951 | - $parts++; |
|
952 | - //wrap parts in AND condition |
|
953 | - if($parts > 1) { |
|
954 | - $filter = '(&' . $filter . ')'; |
|
955 | - } |
|
956 | - break; |
|
957 | - |
|
958 | - case self::LFILTER_LOGIN: |
|
959 | - $ulf = $this->configuration->ldapUserFilter; |
|
960 | - $loginpart = '=%uid'; |
|
961 | - $filterUsername = ''; |
|
962 | - $userAttributes = $this->getUserAttributes(); |
|
963 | - $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
964 | - $parts = 0; |
|
965 | - |
|
966 | - if($this->configuration->ldapLoginFilterUsername === '1') { |
|
967 | - $attr = ''; |
|
968 | - if(isset($userAttributes['uid'])) { |
|
969 | - $attr = 'uid'; |
|
970 | - } else if(isset($userAttributes['samaccountname'])) { |
|
971 | - $attr = 'samaccountname'; |
|
972 | - } else if(isset($userAttributes['cn'])) { |
|
973 | - //fallback |
|
974 | - $attr = 'cn'; |
|
975 | - } |
|
976 | - if ($attr !== '') { |
|
977 | - $filterUsername = '(' . $attr . $loginpart . ')'; |
|
978 | - $parts++; |
|
979 | - } |
|
980 | - } |
|
981 | - |
|
982 | - $filterEmail = ''; |
|
983 | - if($this->configuration->ldapLoginFilterEmail === '1') { |
|
984 | - $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
985 | - $parts++; |
|
986 | - } |
|
987 | - |
|
988 | - $filterAttributes = ''; |
|
989 | - $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
990 | - if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
991 | - $filterAttributes = '(|'; |
|
992 | - foreach($attrsToFilter as $attribute) { |
|
993 | - $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
994 | - } |
|
995 | - $filterAttributes .= ')'; |
|
996 | - $parts++; |
|
997 | - } |
|
998 | - |
|
999 | - $filterLogin = ''; |
|
1000 | - if($parts > 1) { |
|
1001 | - $filterLogin = '(|'; |
|
1002 | - } |
|
1003 | - $filterLogin .= $filterUsername; |
|
1004 | - $filterLogin .= $filterEmail; |
|
1005 | - $filterLogin .= $filterAttributes; |
|
1006 | - if($parts > 1) { |
|
1007 | - $filterLogin .= ')'; |
|
1008 | - } |
|
1009 | - |
|
1010 | - $filter = '(&'.$ulf.$filterLogin.')'; |
|
1011 | - break; |
|
1012 | - } |
|
1013 | - |
|
1014 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
1015 | - |
|
1016 | - return $filter; |
|
1017 | - } |
|
1018 | - |
|
1019 | - /** |
|
1020 | - * Connects and Binds to an LDAP Server |
|
1021 | - * @param int $port the port to connect with |
|
1022 | - * @param bool $tls whether startTLS is to be used |
|
1023 | - * @param bool $ncc |
|
1024 | - * @return bool |
|
1025 | - * @throws \Exception |
|
1026 | - */ |
|
1027 | - private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
|
1028 | - if($ncc) { |
|
1029 | - //No certificate check |
|
1030 | - //FIXME: undo afterwards |
|
1031 | - putenv('LDAPTLS_REQCERT=never'); |
|
1032 | - } |
|
1033 | - |
|
1034 | - //connect, does not really trigger any server communication |
|
1035 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
|
1036 | - $host = $this->configuration->ldapHost; |
|
1037 | - $hostInfo = parse_url($host); |
|
1038 | - if(!$hostInfo) { |
|
1039 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
1040 | - } |
|
1041 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
1042 | - $cr = $this->ldap->connect($host, $port); |
|
1043 | - if(!is_resource($cr)) { |
|
1044 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
1045 | - } |
|
1046 | - |
|
1047 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Setting LDAP Options ', \OCP\Util::DEBUG); |
|
1048 | - //set LDAP options |
|
1049 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
1050 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
1051 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
1052 | - |
|
1053 | - try { |
|
1054 | - if($tls) { |
|
1055 | - $isTlsWorking = @$this->ldap->startTls($cr); |
|
1056 | - if(!$isTlsWorking) { |
|
1057 | - return false; |
|
1058 | - } |
|
1059 | - } |
|
1060 | - |
|
1061 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
1062 | - //interesting part: do the bind! |
|
1063 | - $login = $this->ldap->bind($cr, |
|
1064 | - $this->configuration->ldapAgentName, |
|
1065 | - $this->configuration->ldapAgentPassword |
|
1066 | - ); |
|
1067 | - $errNo = $this->ldap->errno($cr); |
|
1068 | - $error = ldap_error($cr); |
|
1069 | - $this->ldap->unbind($cr); |
|
1070 | - } catch(ServerNotAvailableException $e) { |
|
1071 | - return false; |
|
1072 | - } |
|
1073 | - |
|
1074 | - if($login === true) { |
|
1075 | - $this->ldap->unbind($cr); |
|
1076 | - if($ncc) { |
|
1077 | - throw new \Exception('Certificate cannot be validated.'); |
|
1078 | - } |
|
1079 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
|
1080 | - return true; |
|
1081 | - } |
|
1082 | - |
|
1083 | - if($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
1084 | - //host, port or TLS wrong |
|
1085 | - return false; |
|
1086 | - } else if ($errNo === 2) { |
|
1087 | - return $this->connectAndBind($port, $tls, true); |
|
1088 | - } |
|
1089 | - throw new \Exception($error, $errNo); |
|
1090 | - } |
|
1091 | - |
|
1092 | - /** |
|
1093 | - * checks whether a valid combination of agent and password has been |
|
1094 | - * provided (either two values or nothing for anonymous connect) |
|
1095 | - * @return bool, true if everything is fine, false otherwise |
|
1096 | - */ |
|
1097 | - private function checkAgentRequirements() { |
|
1098 | - $agent = $this->configuration->ldapAgentName; |
|
1099 | - $pwd = $this->configuration->ldapAgentPassword; |
|
1100 | - |
|
1101 | - return |
|
1102 | - ($agent !== '' && $pwd !== '') |
|
1103 | - || ($agent === '' && $pwd === '') |
|
1104 | - ; |
|
1105 | - } |
|
1106 | - |
|
1107 | - /** |
|
1108 | - * @param array $reqs |
|
1109 | - * @return bool |
|
1110 | - */ |
|
1111 | - private function checkRequirements($reqs) { |
|
1112 | - $this->checkAgentRequirements(); |
|
1113 | - foreach($reqs as $option) { |
|
1114 | - $value = $this->configuration->$option; |
|
1115 | - if(empty($value)) { |
|
1116 | - return false; |
|
1117 | - } |
|
1118 | - } |
|
1119 | - return true; |
|
1120 | - } |
|
1121 | - |
|
1122 | - /** |
|
1123 | - * does a cumulativeSearch on LDAP to get different values of a |
|
1124 | - * specified attribute |
|
1125 | - * @param string[] $filters array, the filters that shall be used in the search |
|
1126 | - * @param string $attr the attribute of which a list of values shall be returned |
|
1127 | - * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
1128 | - * The lower, the faster |
|
1129 | - * @param string $maxF string. if not null, this variable will have the filter that |
|
1130 | - * yields most result entries |
|
1131 | - * @return array|false an array with the values on success, false otherwise |
|
1132 | - */ |
|
1133 | - public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
1134 | - $dnRead = array(); |
|
1135 | - $foundItems = array(); |
|
1136 | - $maxEntries = 0; |
|
1137 | - if(!is_array($this->configuration->ldapBase) |
|
1138 | - || !isset($this->configuration->ldapBase[0])) { |
|
1139 | - return false; |
|
1140 | - } |
|
1141 | - $base = $this->configuration->ldapBase[0]; |
|
1142 | - $cr = $this->getConnection(); |
|
1143 | - if(!$this->ldap->isResource($cr)) { |
|
1144 | - return false; |
|
1145 | - } |
|
1146 | - $lastFilter = null; |
|
1147 | - if(isset($filters[count($filters)-1])) { |
|
1148 | - $lastFilter = $filters[count($filters)-1]; |
|
1149 | - } |
|
1150 | - foreach($filters as $filter) { |
|
1151 | - if($lastFilter === $filter && count($foundItems) > 0) { |
|
1152 | - //skip when the filter is a wildcard and results were found |
|
1153 | - continue; |
|
1154 | - } |
|
1155 | - // 20k limit for performance and reason |
|
1156 | - $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
1157 | - if(!$this->ldap->isResource($rr)) { |
|
1158 | - continue; |
|
1159 | - } |
|
1160 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
1161 | - $getEntryFunc = 'firstEntry'; |
|
1162 | - if(($entries !== false) && ($entries > 0)) { |
|
1163 | - if(!is_null($maxF) && $entries > $maxEntries) { |
|
1164 | - $maxEntries = $entries; |
|
1165 | - $maxF = $filter; |
|
1166 | - } |
|
1167 | - $dnReadCount = 0; |
|
1168 | - do { |
|
1169 | - $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
1170 | - $getEntryFunc = 'nextEntry'; |
|
1171 | - if(!$this->ldap->isResource($entry)) { |
|
1172 | - continue 2; |
|
1173 | - } |
|
1174 | - $rr = $entry; //will be expected by nextEntry next round |
|
1175 | - $attributes = $this->ldap->getAttributes($cr, $entry); |
|
1176 | - $dn = $this->ldap->getDN($cr, $entry); |
|
1177 | - if($dn === false || in_array($dn, $dnRead)) { |
|
1178 | - continue; |
|
1179 | - } |
|
1180 | - $newItems = array(); |
|
1181 | - $state = $this->getAttributeValuesFromEntry($attributes, |
|
1182 | - $attr, |
|
1183 | - $newItems); |
|
1184 | - $dnReadCount++; |
|
1185 | - $foundItems = array_merge($foundItems, $newItems); |
|
1186 | - $this->resultCache[$dn][$attr] = $newItems; |
|
1187 | - $dnRead[] = $dn; |
|
1188 | - } while(($state === self::LRESULT_PROCESSED_SKIP |
|
1189 | - || $this->ldap->isResource($entry)) |
|
1190 | - && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
1191 | - } |
|
1192 | - } |
|
1193 | - |
|
1194 | - return array_unique($foundItems); |
|
1195 | - } |
|
1196 | - |
|
1197 | - /** |
|
1198 | - * determines if and which $attr are available on the LDAP server |
|
1199 | - * @param string[] $objectclasses the objectclasses to use as search filter |
|
1200 | - * @param string $attr the attribute to look for |
|
1201 | - * @param string $dbkey the dbkey of the setting the feature is connected to |
|
1202 | - * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
1203 | - * Configuration class |
|
1204 | - * @param bool $po whether the objectClass with most result entries |
|
1205 | - * shall be pre-selected via the result |
|
1206 | - * @return array|false list of found items. |
|
1207 | - * @throws \Exception |
|
1208 | - */ |
|
1209 | - private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
1210 | - $cr = $this->getConnection(); |
|
1211 | - if(!$cr) { |
|
1212 | - throw new \Exception('Could not connect to LDAP'); |
|
1213 | - } |
|
1214 | - $p = 'objectclass='; |
|
1215 | - foreach($objectclasses as $key => $value) { |
|
1216 | - $objectclasses[$key] = $p.$value; |
|
1217 | - } |
|
1218 | - $maxEntryObjC = ''; |
|
1219 | - |
|
1220 | - //how deep to dig? |
|
1221 | - //When looking for objectclasses, testing few entries is sufficient, |
|
1222 | - $dig = 3; |
|
1223 | - |
|
1224 | - $availableFeatures = |
|
1225 | - $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
1226 | - $dig, $maxEntryObjC); |
|
1227 | - if(is_array($availableFeatures) |
|
1228 | - && count($availableFeatures) > 0) { |
|
1229 | - natcasesort($availableFeatures); |
|
1230 | - //natcasesort keeps indices, but we must get rid of them for proper |
|
1231 | - //sorting in the web UI. Therefore: array_values |
|
1232 | - $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
1233 | - } else { |
|
1234 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
1235 | - } |
|
1236 | - |
|
1237 | - $setFeatures = $this->configuration->$confkey; |
|
1238 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
1239 | - //something is already configured? pre-select it. |
|
1240 | - $this->result->addChange($dbkey, $setFeatures); |
|
1241 | - } else if ($po && $maxEntryObjC !== '') { |
|
1242 | - //pre-select objectclass with most result entries |
|
1243 | - $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
1244 | - $this->applyFind($dbkey, $maxEntryObjC); |
|
1245 | - $this->result->addChange($dbkey, $maxEntryObjC); |
|
1246 | - } |
|
1247 | - |
|
1248 | - return $availableFeatures; |
|
1249 | - } |
|
1250 | - |
|
1251 | - /** |
|
1252 | - * appends a list of values fr |
|
1253 | - * @param resource $result the return value from ldap_get_attributes |
|
1254 | - * @param string $attribute the attribute values to look for |
|
1255 | - * @param array &$known new values will be appended here |
|
1256 | - * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
1257 | - * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
1258 | - */ |
|
1259 | - private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
1260 | - if(!is_array($result) |
|
1261 | - || !isset($result['count']) |
|
1262 | - || !$result['count'] > 0) { |
|
1263 | - return self::LRESULT_PROCESSED_INVALID; |
|
1264 | - } |
|
1265 | - |
|
1266 | - // strtolower on all keys for proper comparison |
|
1267 | - $result = \OCP\Util::mb_array_change_key_case($result); |
|
1268 | - $attribute = strtolower($attribute); |
|
1269 | - if(isset($result[$attribute])) { |
|
1270 | - foreach($result[$attribute] as $key => $val) { |
|
1271 | - if($key === 'count') { |
|
1272 | - continue; |
|
1273 | - } |
|
1274 | - if(!in_array($val, $known)) { |
|
1275 | - $known[] = $val; |
|
1276 | - } |
|
1277 | - } |
|
1278 | - return self::LRESULT_PROCESSED_OK; |
|
1279 | - } else { |
|
1280 | - return self::LRESULT_PROCESSED_SKIP; |
|
1281 | - } |
|
1282 | - } |
|
1283 | - |
|
1284 | - /** |
|
1285 | - * @return bool|mixed |
|
1286 | - */ |
|
1287 | - private function getConnection() { |
|
1288 | - if(!is_null($this->cr)) { |
|
1289 | - return $this->cr; |
|
1290 | - } |
|
1291 | - |
|
1292 | - $cr = $this->ldap->connect( |
|
1293 | - $this->configuration->ldapHost, |
|
1294 | - $this->configuration->ldapPort |
|
1295 | - ); |
|
1296 | - |
|
1297 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
1298 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
1299 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
1300 | - if($this->configuration->ldapTLS === 1) { |
|
1301 | - $this->ldap->startTls($cr); |
|
1302 | - } |
|
1303 | - |
|
1304 | - $lo = @$this->ldap->bind($cr, |
|
1305 | - $this->configuration->ldapAgentName, |
|
1306 | - $this->configuration->ldapAgentPassword); |
|
1307 | - if($lo === true) { |
|
1308 | - $this->$cr = $cr; |
|
1309 | - return $cr; |
|
1310 | - } |
|
1311 | - |
|
1312 | - return false; |
|
1313 | - } |
|
1314 | - |
|
1315 | - /** |
|
1316 | - * @return array |
|
1317 | - */ |
|
1318 | - private function getDefaultLdapPortSettings() { |
|
1319 | - static $settings = array( |
|
1320 | - array('port' => 7636, 'tls' => false), |
|
1321 | - array('port' => 636, 'tls' => false), |
|
1322 | - array('port' => 7389, 'tls' => true), |
|
1323 | - array('port' => 389, 'tls' => true), |
|
1324 | - array('port' => 7389, 'tls' => false), |
|
1325 | - array('port' => 389, 'tls' => false), |
|
1326 | - ); |
|
1327 | - return $settings; |
|
1328 | - } |
|
1329 | - |
|
1330 | - /** |
|
1331 | - * @return array |
|
1332 | - */ |
|
1333 | - private function getPortSettingsToTry() { |
|
1334 | - //389 ← LDAP / Unencrypted or StartTLS |
|
1335 | - //636 ← LDAPS / SSL |
|
1336 | - //7xxx ← UCS. need to be checked first, because both ports may be open |
|
1337 | - $host = $this->configuration->ldapHost; |
|
1338 | - $port = intval($this->configuration->ldapPort); |
|
1339 | - $portSettings = array(); |
|
1340 | - |
|
1341 | - //In case the port is already provided, we will check this first |
|
1342 | - if($port > 0) { |
|
1343 | - $hostInfo = parse_url($host); |
|
1344 | - if(!(is_array($hostInfo) |
|
1345 | - && isset($hostInfo['scheme']) |
|
1346 | - && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
1347 | - $portSettings[] = array('port' => $port, 'tls' => true); |
|
1348 | - } |
|
1349 | - $portSettings[] =array('port' => $port, 'tls' => false); |
|
1350 | - } |
|
1351 | - |
|
1352 | - //default ports |
|
1353 | - $portSettings = array_merge($portSettings, |
|
1354 | - $this->getDefaultLdapPortSettings()); |
|
1355 | - |
|
1356 | - return $portSettings; |
|
1357 | - } |
|
40 | + /** @var \OCP\IL10N */ |
|
41 | + static protected $l; |
|
42 | + protected $access; |
|
43 | + protected $cr; |
|
44 | + protected $configuration; |
|
45 | + protected $result; |
|
46 | + protected $resultCache = array(); |
|
47 | + |
|
48 | + const LRESULT_PROCESSED_OK = 2; |
|
49 | + const LRESULT_PROCESSED_INVALID = 3; |
|
50 | + const LRESULT_PROCESSED_SKIP = 4; |
|
51 | + |
|
52 | + const LFILTER_LOGIN = 2; |
|
53 | + const LFILTER_USER_LIST = 3; |
|
54 | + const LFILTER_GROUP_LIST = 4; |
|
55 | + |
|
56 | + const LFILTER_MODE_ASSISTED = 2; |
|
57 | + const LFILTER_MODE_RAW = 1; |
|
58 | + |
|
59 | + const LDAP_NW_TIMEOUT = 4; |
|
60 | + |
|
61 | + /** |
|
62 | + * Constructor |
|
63 | + * @param Configuration $configuration an instance of Configuration |
|
64 | + * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
65 | + * @param Access $access |
|
66 | + */ |
|
67 | + public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
68 | + parent::__construct($ldap); |
|
69 | + $this->configuration = $configuration; |
|
70 | + if(is_null(Wizard::$l)) { |
|
71 | + Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
72 | + } |
|
73 | + $this->access = $access; |
|
74 | + $this->result = new WizardResult(); |
|
75 | + } |
|
76 | + |
|
77 | + public function __destruct() { |
|
78 | + if($this->result->hasChanges()) { |
|
79 | + $this->configuration->saveConfiguration(); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * counts entries in the LDAP directory |
|
85 | + * |
|
86 | + * @param string $filter the LDAP search filter |
|
87 | + * @param string $type a string being either 'users' or 'groups'; |
|
88 | + * @return bool|int |
|
89 | + * @throws \Exception |
|
90 | + */ |
|
91 | + public function countEntries($filter, $type) { |
|
92 | + $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
93 | + if($type === 'users') { |
|
94 | + $reqs[] = 'ldapUserFilter'; |
|
95 | + } |
|
96 | + if(!$this->checkRequirements($reqs)) { |
|
97 | + throw new \Exception('Requirements not met', 400); |
|
98 | + } |
|
99 | + |
|
100 | + $attr = array('dn'); // default |
|
101 | + $limit = 1001; |
|
102 | + if($type === 'groups') { |
|
103 | + $result = $this->access->countGroups($filter, $attr, $limit); |
|
104 | + } else if($type === 'users') { |
|
105 | + $result = $this->access->countUsers($filter, $attr, $limit); |
|
106 | + } else if ($type === 'objects') { |
|
107 | + $result = $this->access->countObjects($limit); |
|
108 | + } else { |
|
109 | + throw new \Exception('internal error: invalid object type', 500); |
|
110 | + } |
|
111 | + |
|
112 | + return $result; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * formats the return value of a count operation to the string to be |
|
117 | + * inserted. |
|
118 | + * |
|
119 | + * @param bool|int $count |
|
120 | + * @return int|string |
|
121 | + */ |
|
122 | + private function formatCountResult($count) { |
|
123 | + $formatted = ($count !== false) ? $count : 0; |
|
124 | + if($formatted > 1000) { |
|
125 | + $formatted = '> 1000'; |
|
126 | + } |
|
127 | + return $formatted; |
|
128 | + } |
|
129 | + |
|
130 | + public function countGroups() { |
|
131 | + $filter = $this->configuration->ldapGroupFilter; |
|
132 | + |
|
133 | + if(empty($filter)) { |
|
134 | + $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
135 | + $this->result->addChange('ldap_group_count', $output); |
|
136 | + return $this->result; |
|
137 | + } |
|
138 | + |
|
139 | + try { |
|
140 | + $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
141 | + } catch (\Exception $e) { |
|
142 | + //400 can be ignored, 500 is forwarded |
|
143 | + if($e->getCode() === 500) { |
|
144 | + throw $e; |
|
145 | + } |
|
146 | + return false; |
|
147 | + } |
|
148 | + $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
149 | + $this->result->addChange('ldap_group_count', $output); |
|
150 | + return $this->result; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return WizardResult |
|
155 | + * @throws \Exception |
|
156 | + */ |
|
157 | + public function countUsers() { |
|
158 | + $filter = $this->access->getFilterForUserCount(); |
|
159 | + |
|
160 | + $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
161 | + $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
162 | + $this->result->addChange('ldap_user_count', $output); |
|
163 | + return $this->result; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * counts any objects in the currently set base dn |
|
168 | + * |
|
169 | + * @return WizardResult |
|
170 | + * @throws \Exception |
|
171 | + */ |
|
172 | + public function countInBaseDN() { |
|
173 | + // we don't need to provide a filter in this case |
|
174 | + $total = $this->countEntries(null, 'objects'); |
|
175 | + if($total === false) { |
|
176 | + throw new \Exception('invalid results received'); |
|
177 | + } |
|
178 | + $this->result->addChange('ldap_test_base', $total); |
|
179 | + return $this->result; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * counts users with a specified attribute |
|
184 | + * @param string $attr |
|
185 | + * @param bool $existsCheck |
|
186 | + * @return int|bool |
|
187 | + */ |
|
188 | + public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
189 | + if(!$this->checkRequirements(array('ldapHost', |
|
190 | + 'ldapPort', |
|
191 | + 'ldapBase', |
|
192 | + 'ldapUserFilter', |
|
193 | + ))) { |
|
194 | + return false; |
|
195 | + } |
|
196 | + |
|
197 | + $filter = $this->access->combineFilterWithAnd(array( |
|
198 | + $this->configuration->ldapUserFilter, |
|
199 | + $attr . '=*' |
|
200 | + )); |
|
201 | + |
|
202 | + $limit = ($existsCheck === false) ? null : 1; |
|
203 | + |
|
204 | + return $this->access->countUsers($filter, array('dn'), $limit); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * detects the display name attribute. If a setting is already present that |
|
209 | + * returns at least one hit, the detection will be canceled. |
|
210 | + * @return WizardResult|bool |
|
211 | + * @throws \Exception |
|
212 | + */ |
|
213 | + public function detectUserDisplayNameAttribute() { |
|
214 | + if(!$this->checkRequirements(array('ldapHost', |
|
215 | + 'ldapPort', |
|
216 | + 'ldapBase', |
|
217 | + 'ldapUserFilter', |
|
218 | + ))) { |
|
219 | + return false; |
|
220 | + } |
|
221 | + |
|
222 | + $attr = $this->configuration->ldapUserDisplayName; |
|
223 | + if ($attr !== '' && $attr !== 'displayName') { |
|
224 | + // most likely not the default value with upper case N, |
|
225 | + // verify it still produces a result |
|
226 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
227 | + if($count > 0) { |
|
228 | + //no change, but we sent it back to make sure the user interface |
|
229 | + //is still correct, even if the ajax call was cancelled meanwhile |
|
230 | + $this->result->addChange('ldap_display_name', $attr); |
|
231 | + return $this->result; |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + // first attribute that has at least one result wins |
|
236 | + $displayNameAttrs = array('displayname', 'cn'); |
|
237 | + foreach ($displayNameAttrs as $attr) { |
|
238 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
239 | + |
|
240 | + if($count > 0) { |
|
241 | + $this->applyFind('ldap_display_name', $attr); |
|
242 | + return $this->result; |
|
243 | + } |
|
244 | + }; |
|
245 | + |
|
246 | + throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.')); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * detects the most often used email attribute for users applying to the |
|
251 | + * user list filter. If a setting is already present that returns at least |
|
252 | + * one hit, the detection will be canceled. |
|
253 | + * @return WizardResult|bool |
|
254 | + */ |
|
255 | + public function detectEmailAttribute() { |
|
256 | + if(!$this->checkRequirements(array('ldapHost', |
|
257 | + 'ldapPort', |
|
258 | + 'ldapBase', |
|
259 | + 'ldapUserFilter', |
|
260 | + ))) { |
|
261 | + return false; |
|
262 | + } |
|
263 | + |
|
264 | + $attr = $this->configuration->ldapEmailAttribute; |
|
265 | + if ($attr !== '') { |
|
266 | + $count = intval($this->countUsersWithAttribute($attr, true)); |
|
267 | + if($count > 0) { |
|
268 | + return false; |
|
269 | + } |
|
270 | + $writeLog = true; |
|
271 | + } else { |
|
272 | + $writeLog = false; |
|
273 | + } |
|
274 | + |
|
275 | + $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
276 | + $winner = ''; |
|
277 | + $maxUsers = 0; |
|
278 | + foreach($emailAttributes as $attr) { |
|
279 | + $count = $this->countUsersWithAttribute($attr); |
|
280 | + if($count > $maxUsers) { |
|
281 | + $maxUsers = $count; |
|
282 | + $winner = $attr; |
|
283 | + } |
|
284 | + } |
|
285 | + |
|
286 | + if($winner !== '') { |
|
287 | + $this->applyFind('ldap_email_attr', $winner); |
|
288 | + if($writeLog) { |
|
289 | + \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
290 | + 'automatically been reset, because the original value ' . |
|
291 | + 'did not return any results.', \OCP\Util::INFO); |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + return $this->result; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * @return WizardResult |
|
300 | + * @throws \Exception |
|
301 | + */ |
|
302 | + public function determineAttributes() { |
|
303 | + if(!$this->checkRequirements(array('ldapHost', |
|
304 | + 'ldapPort', |
|
305 | + 'ldapBase', |
|
306 | + 'ldapUserFilter', |
|
307 | + ))) { |
|
308 | + return false; |
|
309 | + } |
|
310 | + |
|
311 | + $attributes = $this->getUserAttributes(); |
|
312 | + |
|
313 | + natcasesort($attributes); |
|
314 | + $attributes = array_values($attributes); |
|
315 | + |
|
316 | + $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
317 | + |
|
318 | + $selected = $this->configuration->ldapLoginFilterAttributes; |
|
319 | + if(is_array($selected) && !empty($selected)) { |
|
320 | + $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
321 | + } |
|
322 | + |
|
323 | + return $this->result; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * detects the available LDAP attributes |
|
328 | + * @return array|false The instance's WizardResult instance |
|
329 | + * @throws \Exception |
|
330 | + */ |
|
331 | + private function getUserAttributes() { |
|
332 | + if(!$this->checkRequirements(array('ldapHost', |
|
333 | + 'ldapPort', |
|
334 | + 'ldapBase', |
|
335 | + 'ldapUserFilter', |
|
336 | + ))) { |
|
337 | + return false; |
|
338 | + } |
|
339 | + $cr = $this->getConnection(); |
|
340 | + if(!$cr) { |
|
341 | + throw new \Exception('Could not connect to LDAP'); |
|
342 | + } |
|
343 | + |
|
344 | + $base = $this->configuration->ldapBase[0]; |
|
345 | + $filter = $this->configuration->ldapUserFilter; |
|
346 | + $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
347 | + if(!$this->ldap->isResource($rr)) { |
|
348 | + return false; |
|
349 | + } |
|
350 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
351 | + $attributes = $this->ldap->getAttributes($cr, $er); |
|
352 | + $pureAttributes = array(); |
|
353 | + for($i = 0; $i < $attributes['count']; $i++) { |
|
354 | + $pureAttributes[] = $attributes[$i]; |
|
355 | + } |
|
356 | + |
|
357 | + return $pureAttributes; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * detects the available LDAP groups |
|
362 | + * @return WizardResult|false the instance's WizardResult instance |
|
363 | + */ |
|
364 | + public function determineGroupsForGroups() { |
|
365 | + return $this->determineGroups('ldap_groupfilter_groups', |
|
366 | + 'ldapGroupFilterGroups', |
|
367 | + false); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * detects the available LDAP groups |
|
372 | + * @return WizardResult|false the instance's WizardResult instance |
|
373 | + */ |
|
374 | + public function determineGroupsForUsers() { |
|
375 | + return $this->determineGroups('ldap_userfilter_groups', |
|
376 | + 'ldapUserFilterGroups'); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * detects the available LDAP groups |
|
381 | + * @param string $dbKey |
|
382 | + * @param string $confKey |
|
383 | + * @param bool $testMemberOf |
|
384 | + * @return WizardResult|false the instance's WizardResult instance |
|
385 | + * @throws \Exception |
|
386 | + */ |
|
387 | + private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
388 | + if(!$this->checkRequirements(array('ldapHost', |
|
389 | + 'ldapPort', |
|
390 | + 'ldapBase', |
|
391 | + ))) { |
|
392 | + return false; |
|
393 | + } |
|
394 | + $cr = $this->getConnection(); |
|
395 | + if(!$cr) { |
|
396 | + throw new \Exception('Could not connect to LDAP'); |
|
397 | + } |
|
398 | + |
|
399 | + $this->fetchGroups($dbKey, $confKey); |
|
400 | + |
|
401 | + if($testMemberOf) { |
|
402 | + $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
403 | + $this->result->markChange(); |
|
404 | + if(!$this->configuration->hasMemberOfFilterSupport) { |
|
405 | + throw new \Exception('memberOf is not supported by the server'); |
|
406 | + } |
|
407 | + } |
|
408 | + |
|
409 | + return $this->result; |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * fetches all groups from LDAP and adds them to the result object |
|
414 | + * |
|
415 | + * @param string $dbKey |
|
416 | + * @param string $confKey |
|
417 | + * @return array $groupEntries |
|
418 | + * @throws \Exception |
|
419 | + */ |
|
420 | + public function fetchGroups($dbKey, $confKey) { |
|
421 | + $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
|
422 | + |
|
423 | + $filterParts = array(); |
|
424 | + foreach($obclasses as $obclass) { |
|
425 | + $filterParts[] = 'objectclass='.$obclass; |
|
426 | + } |
|
427 | + //we filter for everything |
|
428 | + //- that looks like a group and |
|
429 | + //- has the group display name set |
|
430 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
431 | + $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
432 | + |
|
433 | + $groupNames = array(); |
|
434 | + $groupEntries = array(); |
|
435 | + $limit = 400; |
|
436 | + $offset = 0; |
|
437 | + do { |
|
438 | + // we need to request dn additionally here, otherwise memberOf |
|
439 | + // detection will fail later |
|
440 | + $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
441 | + foreach($result as $item) { |
|
442 | + if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
443 | + // just in case - no issue known |
|
444 | + continue; |
|
445 | + } |
|
446 | + $groupNames[] = $item['cn'][0]; |
|
447 | + $groupEntries[] = $item; |
|
448 | + } |
|
449 | + $offset += $limit; |
|
450 | + } while ($this->access->hasMoreResults()); |
|
451 | + |
|
452 | + if(count($groupNames) > 0) { |
|
453 | + natsort($groupNames); |
|
454 | + $this->result->addOptions($dbKey, array_values($groupNames)); |
|
455 | + } else { |
|
456 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
457 | + } |
|
458 | + |
|
459 | + $setFeatures = $this->configuration->$confKey; |
|
460 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
461 | + //something is already configured? pre-select it. |
|
462 | + $this->result->addChange($dbKey, $setFeatures); |
|
463 | + } |
|
464 | + return $groupEntries; |
|
465 | + } |
|
466 | + |
|
467 | + public function determineGroupMemberAssoc() { |
|
468 | + if(!$this->checkRequirements(array('ldapHost', |
|
469 | + 'ldapPort', |
|
470 | + 'ldapGroupFilter', |
|
471 | + ))) { |
|
472 | + return false; |
|
473 | + } |
|
474 | + $attribute = $this->detectGroupMemberAssoc(); |
|
475 | + if($attribute === false) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
479 | + $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
480 | + |
|
481 | + return $this->result; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Detects the available object classes |
|
486 | + * @return WizardResult|false the instance's WizardResult instance |
|
487 | + * @throws \Exception |
|
488 | + */ |
|
489 | + public function determineGroupObjectClasses() { |
|
490 | + if(!$this->checkRequirements(array('ldapHost', |
|
491 | + 'ldapPort', |
|
492 | + 'ldapBase', |
|
493 | + ))) { |
|
494 | + return false; |
|
495 | + } |
|
496 | + $cr = $this->getConnection(); |
|
497 | + if(!$cr) { |
|
498 | + throw new \Exception('Could not connect to LDAP'); |
|
499 | + } |
|
500 | + |
|
501 | + $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
502 | + $this->determineFeature($obclasses, |
|
503 | + 'objectclass', |
|
504 | + 'ldap_groupfilter_objectclass', |
|
505 | + 'ldapGroupFilterObjectclass', |
|
506 | + false); |
|
507 | + |
|
508 | + return $this->result; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * detects the available object classes |
|
513 | + * @return WizardResult |
|
514 | + * @throws \Exception |
|
515 | + */ |
|
516 | + public function determineUserObjectClasses() { |
|
517 | + if(!$this->checkRequirements(array('ldapHost', |
|
518 | + 'ldapPort', |
|
519 | + 'ldapBase', |
|
520 | + ))) { |
|
521 | + return false; |
|
522 | + } |
|
523 | + $cr = $this->getConnection(); |
|
524 | + if(!$cr) { |
|
525 | + throw new \Exception('Could not connect to LDAP'); |
|
526 | + } |
|
527 | + |
|
528 | + $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
529 | + 'user', 'posixAccount', '*'); |
|
530 | + $filter = $this->configuration->ldapUserFilter; |
|
531 | + //if filter is empty, it is probably the first time the wizard is called |
|
532 | + //then, apply suggestions. |
|
533 | + $this->determineFeature($obclasses, |
|
534 | + 'objectclass', |
|
535 | + 'ldap_userfilter_objectclass', |
|
536 | + 'ldapUserFilterObjectclass', |
|
537 | + empty($filter)); |
|
538 | + |
|
539 | + return $this->result; |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * @return WizardResult|false |
|
544 | + * @throws \Exception |
|
545 | + */ |
|
546 | + public function getGroupFilter() { |
|
547 | + if(!$this->checkRequirements(array('ldapHost', |
|
548 | + 'ldapPort', |
|
549 | + 'ldapBase', |
|
550 | + ))) { |
|
551 | + return false; |
|
552 | + } |
|
553 | + //make sure the use display name is set |
|
554 | + $displayName = $this->configuration->ldapGroupDisplayName; |
|
555 | + if ($displayName === '') { |
|
556 | + $d = $this->configuration->getDefaults(); |
|
557 | + $this->applyFind('ldap_group_display_name', |
|
558 | + $d['ldap_group_display_name']); |
|
559 | + } |
|
560 | + $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
561 | + |
|
562 | + $this->applyFind('ldap_group_filter', $filter); |
|
563 | + return $this->result; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * @return WizardResult|false |
|
568 | + * @throws \Exception |
|
569 | + */ |
|
570 | + public function getUserListFilter() { |
|
571 | + if(!$this->checkRequirements(array('ldapHost', |
|
572 | + 'ldapPort', |
|
573 | + 'ldapBase', |
|
574 | + ))) { |
|
575 | + return false; |
|
576 | + } |
|
577 | + //make sure the use display name is set |
|
578 | + $displayName = $this->configuration->ldapUserDisplayName; |
|
579 | + if ($displayName === '') { |
|
580 | + $d = $this->configuration->getDefaults(); |
|
581 | + $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
582 | + } |
|
583 | + $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
584 | + if(!$filter) { |
|
585 | + throw new \Exception('Cannot create filter'); |
|
586 | + } |
|
587 | + |
|
588 | + $this->applyFind('ldap_userlist_filter', $filter); |
|
589 | + return $this->result; |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * @return bool|WizardResult |
|
594 | + * @throws \Exception |
|
595 | + */ |
|
596 | + public function getUserLoginFilter() { |
|
597 | + if(!$this->checkRequirements(array('ldapHost', |
|
598 | + 'ldapPort', |
|
599 | + 'ldapBase', |
|
600 | + 'ldapUserFilter', |
|
601 | + ))) { |
|
602 | + return false; |
|
603 | + } |
|
604 | + |
|
605 | + $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
606 | + if(!$filter) { |
|
607 | + throw new \Exception('Cannot create filter'); |
|
608 | + } |
|
609 | + |
|
610 | + $this->applyFind('ldap_login_filter', $filter); |
|
611 | + return $this->result; |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * @return bool|WizardResult |
|
616 | + * @param string $loginName |
|
617 | + * @throws \Exception |
|
618 | + */ |
|
619 | + public function testLoginName($loginName) { |
|
620 | + if(!$this->checkRequirements(array('ldapHost', |
|
621 | + 'ldapPort', |
|
622 | + 'ldapBase', |
|
623 | + 'ldapLoginFilter', |
|
624 | + ))) { |
|
625 | + return false; |
|
626 | + } |
|
627 | + |
|
628 | + $cr = $this->access->connection->getConnectionResource(); |
|
629 | + if(!$this->ldap->isResource($cr)) { |
|
630 | + throw new \Exception('connection error'); |
|
631 | + } |
|
632 | + |
|
633 | + if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
634 | + === false) { |
|
635 | + throw new \Exception('missing placeholder'); |
|
636 | + } |
|
637 | + |
|
638 | + $users = $this->access->countUsersByLoginName($loginName); |
|
639 | + if($this->ldap->errno($cr) !== 0) { |
|
640 | + throw new \Exception($this->ldap->error($cr)); |
|
641 | + } |
|
642 | + $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
643 | + $this->result->addChange('ldap_test_loginname', $users); |
|
644 | + $this->result->addChange('ldap_test_effective_filter', $filter); |
|
645 | + return $this->result; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Tries to determine the port, requires given Host, User DN and Password |
|
650 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
651 | + * @throws \Exception |
|
652 | + */ |
|
653 | + public function guessPortAndTLS() { |
|
654 | + if(!$this->checkRequirements(array('ldapHost', |
|
655 | + ))) { |
|
656 | + return false; |
|
657 | + } |
|
658 | + $this->checkHost(); |
|
659 | + $portSettings = $this->getPortSettingsToTry(); |
|
660 | + |
|
661 | + if(!is_array($portSettings)) { |
|
662 | + throw new \Exception(print_r($portSettings, true)); |
|
663 | + } |
|
664 | + |
|
665 | + //proceed from the best configuration and return on first success |
|
666 | + foreach($portSettings as $setting) { |
|
667 | + $p = $setting['port']; |
|
668 | + $t = $setting['tls']; |
|
669 | + \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
670 | + //connectAndBind may throw Exception, it needs to be catched by the |
|
671 | + //callee of this method |
|
672 | + |
|
673 | + try { |
|
674 | + $settingsFound = $this->connectAndBind($p, $t); |
|
675 | + } catch (\Exception $e) { |
|
676 | + // any reply other than -1 (= cannot connect) is already okay, |
|
677 | + // because then we found the server |
|
678 | + // unavailable startTLS returns -11 |
|
679 | + if($e->getCode() > 0) { |
|
680 | + $settingsFound = true; |
|
681 | + } else { |
|
682 | + throw $e; |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + if ($settingsFound === true) { |
|
687 | + $config = array( |
|
688 | + 'ldapPort' => $p, |
|
689 | + 'ldapTLS' => intval($t) |
|
690 | + ); |
|
691 | + $this->configuration->setConfiguration($config); |
|
692 | + \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
693 | + $this->result->addChange('ldap_port', $p); |
|
694 | + return $this->result; |
|
695 | + } |
|
696 | + } |
|
697 | + |
|
698 | + //custom port, undetected (we do not brute force) |
|
699 | + return false; |
|
700 | + } |
|
701 | + |
|
702 | + /** |
|
703 | + * tries to determine a base dn from User DN or LDAP Host |
|
704 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
705 | + */ |
|
706 | + public function guessBaseDN() { |
|
707 | + if(!$this->checkRequirements(array('ldapHost', |
|
708 | + 'ldapPort', |
|
709 | + ))) { |
|
710 | + return false; |
|
711 | + } |
|
712 | + |
|
713 | + //check whether a DN is given in the agent name (99.9% of all cases) |
|
714 | + $base = null; |
|
715 | + $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
716 | + if($i !== false) { |
|
717 | + $base = substr($this->configuration->ldapAgentName, $i); |
|
718 | + if($this->testBaseDN($base)) { |
|
719 | + $this->applyFind('ldap_base', $base); |
|
720 | + return $this->result; |
|
721 | + } |
|
722 | + } |
|
723 | + |
|
724 | + //this did not help :( |
|
725 | + //Let's see whether we can parse the Host URL and convert the domain to |
|
726 | + //a base DN |
|
727 | + $helper = new Helper(\OC::$server->getConfig()); |
|
728 | + $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
729 | + if(!$domain) { |
|
730 | + return false; |
|
731 | + } |
|
732 | + |
|
733 | + $dparts = explode('.', $domain); |
|
734 | + while(count($dparts) > 0) { |
|
735 | + $base2 = 'dc=' . implode(',dc=', $dparts); |
|
736 | + if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
737 | + $this->applyFind('ldap_base', $base2); |
|
738 | + return $this->result; |
|
739 | + } |
|
740 | + array_shift($dparts); |
|
741 | + } |
|
742 | + |
|
743 | + return false; |
|
744 | + } |
|
745 | + |
|
746 | + /** |
|
747 | + * sets the found value for the configuration key in the WizardResult |
|
748 | + * as well as in the Configuration instance |
|
749 | + * @param string $key the configuration key |
|
750 | + * @param string $value the (detected) value |
|
751 | + * |
|
752 | + */ |
|
753 | + private function applyFind($key, $value) { |
|
754 | + $this->result->addChange($key, $value); |
|
755 | + $this->configuration->setConfiguration(array($key => $value)); |
|
756 | + } |
|
757 | + |
|
758 | + /** |
|
759 | + * Checks, whether a port was entered in the Host configuration |
|
760 | + * field. In this case the port will be stripped off, but also stored as |
|
761 | + * setting. |
|
762 | + */ |
|
763 | + private function checkHost() { |
|
764 | + $host = $this->configuration->ldapHost; |
|
765 | + $hostInfo = parse_url($host); |
|
766 | + |
|
767 | + //removes Port from Host |
|
768 | + if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
769 | + $port = $hostInfo['port']; |
|
770 | + $host = str_replace(':'.$port, '', $host); |
|
771 | + $this->applyFind('ldap_host', $host); |
|
772 | + $this->applyFind('ldap_port', $port); |
|
773 | + } |
|
774 | + } |
|
775 | + |
|
776 | + /** |
|
777 | + * tries to detect the group member association attribute which is |
|
778 | + * one of 'uniqueMember', 'memberUid', 'member' |
|
779 | + * @return string|false, string with the attribute name, false on error |
|
780 | + * @throws \Exception |
|
781 | + */ |
|
782 | + private function detectGroupMemberAssoc() { |
|
783 | + $possibleAttrs = array('uniqueMember', 'memberUid', 'member'); |
|
784 | + $filter = $this->configuration->ldapGroupFilter; |
|
785 | + if(empty($filter)) { |
|
786 | + return false; |
|
787 | + } |
|
788 | + $cr = $this->getConnection(); |
|
789 | + if(!$cr) { |
|
790 | + throw new \Exception('Could not connect to LDAP'); |
|
791 | + } |
|
792 | + $base = $this->configuration->ldapBase[0]; |
|
793 | + $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
794 | + if(!$this->ldap->isResource($rr)) { |
|
795 | + return false; |
|
796 | + } |
|
797 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
798 | + while(is_resource($er)) { |
|
799 | + $this->ldap->getDN($cr, $er); |
|
800 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
801 | + $result = array(); |
|
802 | + $possibleAttrsCount = count($possibleAttrs); |
|
803 | + for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
804 | + if(isset($attrs[$possibleAttrs[$i]])) { |
|
805 | + $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
806 | + } |
|
807 | + } |
|
808 | + if(!empty($result)) { |
|
809 | + natsort($result); |
|
810 | + return key($result); |
|
811 | + } |
|
812 | + |
|
813 | + $er = $this->ldap->nextEntry($cr, $er); |
|
814 | + } |
|
815 | + |
|
816 | + return false; |
|
817 | + } |
|
818 | + |
|
819 | + /** |
|
820 | + * Checks whether for a given BaseDN results will be returned |
|
821 | + * @param string $base the BaseDN to test |
|
822 | + * @return bool true on success, false otherwise |
|
823 | + * @throws \Exception |
|
824 | + */ |
|
825 | + private function testBaseDN($base) { |
|
826 | + $cr = $this->getConnection(); |
|
827 | + if(!$cr) { |
|
828 | + throw new \Exception('Could not connect to LDAP'); |
|
829 | + } |
|
830 | + |
|
831 | + //base is there, let's validate it. If we search for anything, we should |
|
832 | + //get a result set > 0 on a proper base |
|
833 | + $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
834 | + if(!$this->ldap->isResource($rr)) { |
|
835 | + $errorNo = $this->ldap->errno($cr); |
|
836 | + $errorMsg = $this->ldap->error($cr); |
|
837 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
838 | + ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
839 | + return false; |
|
840 | + } |
|
841 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
842 | + return ($entries !== false) && ($entries > 0); |
|
843 | + } |
|
844 | + |
|
845 | + /** |
|
846 | + * Checks whether the server supports memberOf in LDAP Filter. |
|
847 | + * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
848 | + * a configured objectClass. I.e. not necessarily for all available groups |
|
849 | + * memberOf does work. |
|
850 | + * |
|
851 | + * @return bool true if it does, false otherwise |
|
852 | + * @throws \Exception |
|
853 | + */ |
|
854 | + private function testMemberOf() { |
|
855 | + $cr = $this->getConnection(); |
|
856 | + if(!$cr) { |
|
857 | + throw new \Exception('Could not connect to LDAP'); |
|
858 | + } |
|
859 | + $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
860 | + if(is_int($result) && $result > 0) { |
|
861 | + return true; |
|
862 | + } |
|
863 | + return false; |
|
864 | + } |
|
865 | + |
|
866 | + /** |
|
867 | + * creates an LDAP Filter from given configuration |
|
868 | + * @param integer $filterType int, for which use case the filter shall be created |
|
869 | + * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
870 | + * self::LFILTER_GROUP_LIST |
|
871 | + * @return string|false string with the filter on success, false otherwise |
|
872 | + * @throws \Exception |
|
873 | + */ |
|
874 | + private function composeLdapFilter($filterType) { |
|
875 | + $filter = ''; |
|
876 | + $parts = 0; |
|
877 | + switch ($filterType) { |
|
878 | + case self::LFILTER_USER_LIST: |
|
879 | + $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
880 | + //glue objectclasses |
|
881 | + if(is_array($objcs) && count($objcs) > 0) { |
|
882 | + $filter .= '(|'; |
|
883 | + foreach($objcs as $objc) { |
|
884 | + $filter .= '(objectclass=' . $objc . ')'; |
|
885 | + } |
|
886 | + $filter .= ')'; |
|
887 | + $parts++; |
|
888 | + } |
|
889 | + //glue group memberships |
|
890 | + if($this->configuration->hasMemberOfFilterSupport) { |
|
891 | + $cns = $this->configuration->ldapUserFilterGroups; |
|
892 | + if(is_array($cns) && count($cns) > 0) { |
|
893 | + $filter .= '(|'; |
|
894 | + $cr = $this->getConnection(); |
|
895 | + if(!$cr) { |
|
896 | + throw new \Exception('Could not connect to LDAP'); |
|
897 | + } |
|
898 | + $base = $this->configuration->ldapBase[0]; |
|
899 | + foreach($cns as $cn) { |
|
900 | + $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
901 | + if(!$this->ldap->isResource($rr)) { |
|
902 | + continue; |
|
903 | + } |
|
904 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
905 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
906 | + $dn = $this->ldap->getDN($cr, $er); |
|
907 | + if ($dn == false || $dn === '') { |
|
908 | + continue; |
|
909 | + } |
|
910 | + $filterPart = '(memberof=' . $dn . ')'; |
|
911 | + if(isset($attrs['primaryGroupToken'])) { |
|
912 | + $pgt = $attrs['primaryGroupToken'][0]; |
|
913 | + $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
914 | + $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
915 | + } |
|
916 | + $filter .= $filterPart; |
|
917 | + } |
|
918 | + $filter .= ')'; |
|
919 | + } |
|
920 | + $parts++; |
|
921 | + } |
|
922 | + //wrap parts in AND condition |
|
923 | + if($parts > 1) { |
|
924 | + $filter = '(&' . $filter . ')'; |
|
925 | + } |
|
926 | + if ($filter === '') { |
|
927 | + $filter = '(objectclass=*)'; |
|
928 | + } |
|
929 | + break; |
|
930 | + |
|
931 | + case self::LFILTER_GROUP_LIST: |
|
932 | + $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
933 | + //glue objectclasses |
|
934 | + if(is_array($objcs) && count($objcs) > 0) { |
|
935 | + $filter .= '(|'; |
|
936 | + foreach($objcs as $objc) { |
|
937 | + $filter .= '(objectclass=' . $objc . ')'; |
|
938 | + } |
|
939 | + $filter .= ')'; |
|
940 | + $parts++; |
|
941 | + } |
|
942 | + //glue group memberships |
|
943 | + $cns = $this->configuration->ldapGroupFilterGroups; |
|
944 | + if(is_array($cns) && count($cns) > 0) { |
|
945 | + $filter .= '(|'; |
|
946 | + foreach($cns as $cn) { |
|
947 | + $filter .= '(cn=' . $cn . ')'; |
|
948 | + } |
|
949 | + $filter .= ')'; |
|
950 | + } |
|
951 | + $parts++; |
|
952 | + //wrap parts in AND condition |
|
953 | + if($parts > 1) { |
|
954 | + $filter = '(&' . $filter . ')'; |
|
955 | + } |
|
956 | + break; |
|
957 | + |
|
958 | + case self::LFILTER_LOGIN: |
|
959 | + $ulf = $this->configuration->ldapUserFilter; |
|
960 | + $loginpart = '=%uid'; |
|
961 | + $filterUsername = ''; |
|
962 | + $userAttributes = $this->getUserAttributes(); |
|
963 | + $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
964 | + $parts = 0; |
|
965 | + |
|
966 | + if($this->configuration->ldapLoginFilterUsername === '1') { |
|
967 | + $attr = ''; |
|
968 | + if(isset($userAttributes['uid'])) { |
|
969 | + $attr = 'uid'; |
|
970 | + } else if(isset($userAttributes['samaccountname'])) { |
|
971 | + $attr = 'samaccountname'; |
|
972 | + } else if(isset($userAttributes['cn'])) { |
|
973 | + //fallback |
|
974 | + $attr = 'cn'; |
|
975 | + } |
|
976 | + if ($attr !== '') { |
|
977 | + $filterUsername = '(' . $attr . $loginpart . ')'; |
|
978 | + $parts++; |
|
979 | + } |
|
980 | + } |
|
981 | + |
|
982 | + $filterEmail = ''; |
|
983 | + if($this->configuration->ldapLoginFilterEmail === '1') { |
|
984 | + $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
985 | + $parts++; |
|
986 | + } |
|
987 | + |
|
988 | + $filterAttributes = ''; |
|
989 | + $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
990 | + if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
991 | + $filterAttributes = '(|'; |
|
992 | + foreach($attrsToFilter as $attribute) { |
|
993 | + $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
994 | + } |
|
995 | + $filterAttributes .= ')'; |
|
996 | + $parts++; |
|
997 | + } |
|
998 | + |
|
999 | + $filterLogin = ''; |
|
1000 | + if($parts > 1) { |
|
1001 | + $filterLogin = '(|'; |
|
1002 | + } |
|
1003 | + $filterLogin .= $filterUsername; |
|
1004 | + $filterLogin .= $filterEmail; |
|
1005 | + $filterLogin .= $filterAttributes; |
|
1006 | + if($parts > 1) { |
|
1007 | + $filterLogin .= ')'; |
|
1008 | + } |
|
1009 | + |
|
1010 | + $filter = '(&'.$ulf.$filterLogin.')'; |
|
1011 | + break; |
|
1012 | + } |
|
1013 | + |
|
1014 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
1015 | + |
|
1016 | + return $filter; |
|
1017 | + } |
|
1018 | + |
|
1019 | + /** |
|
1020 | + * Connects and Binds to an LDAP Server |
|
1021 | + * @param int $port the port to connect with |
|
1022 | + * @param bool $tls whether startTLS is to be used |
|
1023 | + * @param bool $ncc |
|
1024 | + * @return bool |
|
1025 | + * @throws \Exception |
|
1026 | + */ |
|
1027 | + private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
|
1028 | + if($ncc) { |
|
1029 | + //No certificate check |
|
1030 | + //FIXME: undo afterwards |
|
1031 | + putenv('LDAPTLS_REQCERT=never'); |
|
1032 | + } |
|
1033 | + |
|
1034 | + //connect, does not really trigger any server communication |
|
1035 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
|
1036 | + $host = $this->configuration->ldapHost; |
|
1037 | + $hostInfo = parse_url($host); |
|
1038 | + if(!$hostInfo) { |
|
1039 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
1040 | + } |
|
1041 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
1042 | + $cr = $this->ldap->connect($host, $port); |
|
1043 | + if(!is_resource($cr)) { |
|
1044 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
1045 | + } |
|
1046 | + |
|
1047 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Setting LDAP Options ', \OCP\Util::DEBUG); |
|
1048 | + //set LDAP options |
|
1049 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
1050 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
1051 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
1052 | + |
|
1053 | + try { |
|
1054 | + if($tls) { |
|
1055 | + $isTlsWorking = @$this->ldap->startTls($cr); |
|
1056 | + if(!$isTlsWorking) { |
|
1057 | + return false; |
|
1058 | + } |
|
1059 | + } |
|
1060 | + |
|
1061 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
1062 | + //interesting part: do the bind! |
|
1063 | + $login = $this->ldap->bind($cr, |
|
1064 | + $this->configuration->ldapAgentName, |
|
1065 | + $this->configuration->ldapAgentPassword |
|
1066 | + ); |
|
1067 | + $errNo = $this->ldap->errno($cr); |
|
1068 | + $error = ldap_error($cr); |
|
1069 | + $this->ldap->unbind($cr); |
|
1070 | + } catch(ServerNotAvailableException $e) { |
|
1071 | + return false; |
|
1072 | + } |
|
1073 | + |
|
1074 | + if($login === true) { |
|
1075 | + $this->ldap->unbind($cr); |
|
1076 | + if($ncc) { |
|
1077 | + throw new \Exception('Certificate cannot be validated.'); |
|
1078 | + } |
|
1079 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
|
1080 | + return true; |
|
1081 | + } |
|
1082 | + |
|
1083 | + if($errNo === -1 || ($errNo === 2 && $ncc)) { |
|
1084 | + //host, port or TLS wrong |
|
1085 | + return false; |
|
1086 | + } else if ($errNo === 2) { |
|
1087 | + return $this->connectAndBind($port, $tls, true); |
|
1088 | + } |
|
1089 | + throw new \Exception($error, $errNo); |
|
1090 | + } |
|
1091 | + |
|
1092 | + /** |
|
1093 | + * checks whether a valid combination of agent and password has been |
|
1094 | + * provided (either two values or nothing for anonymous connect) |
|
1095 | + * @return bool, true if everything is fine, false otherwise |
|
1096 | + */ |
|
1097 | + private function checkAgentRequirements() { |
|
1098 | + $agent = $this->configuration->ldapAgentName; |
|
1099 | + $pwd = $this->configuration->ldapAgentPassword; |
|
1100 | + |
|
1101 | + return |
|
1102 | + ($agent !== '' && $pwd !== '') |
|
1103 | + || ($agent === '' && $pwd === '') |
|
1104 | + ; |
|
1105 | + } |
|
1106 | + |
|
1107 | + /** |
|
1108 | + * @param array $reqs |
|
1109 | + * @return bool |
|
1110 | + */ |
|
1111 | + private function checkRequirements($reqs) { |
|
1112 | + $this->checkAgentRequirements(); |
|
1113 | + foreach($reqs as $option) { |
|
1114 | + $value = $this->configuration->$option; |
|
1115 | + if(empty($value)) { |
|
1116 | + return false; |
|
1117 | + } |
|
1118 | + } |
|
1119 | + return true; |
|
1120 | + } |
|
1121 | + |
|
1122 | + /** |
|
1123 | + * does a cumulativeSearch on LDAP to get different values of a |
|
1124 | + * specified attribute |
|
1125 | + * @param string[] $filters array, the filters that shall be used in the search |
|
1126 | + * @param string $attr the attribute of which a list of values shall be returned |
|
1127 | + * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
1128 | + * The lower, the faster |
|
1129 | + * @param string $maxF string. if not null, this variable will have the filter that |
|
1130 | + * yields most result entries |
|
1131 | + * @return array|false an array with the values on success, false otherwise |
|
1132 | + */ |
|
1133 | + public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
1134 | + $dnRead = array(); |
|
1135 | + $foundItems = array(); |
|
1136 | + $maxEntries = 0; |
|
1137 | + if(!is_array($this->configuration->ldapBase) |
|
1138 | + || !isset($this->configuration->ldapBase[0])) { |
|
1139 | + return false; |
|
1140 | + } |
|
1141 | + $base = $this->configuration->ldapBase[0]; |
|
1142 | + $cr = $this->getConnection(); |
|
1143 | + if(!$this->ldap->isResource($cr)) { |
|
1144 | + return false; |
|
1145 | + } |
|
1146 | + $lastFilter = null; |
|
1147 | + if(isset($filters[count($filters)-1])) { |
|
1148 | + $lastFilter = $filters[count($filters)-1]; |
|
1149 | + } |
|
1150 | + foreach($filters as $filter) { |
|
1151 | + if($lastFilter === $filter && count($foundItems) > 0) { |
|
1152 | + //skip when the filter is a wildcard and results were found |
|
1153 | + continue; |
|
1154 | + } |
|
1155 | + // 20k limit for performance and reason |
|
1156 | + $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
1157 | + if(!$this->ldap->isResource($rr)) { |
|
1158 | + continue; |
|
1159 | + } |
|
1160 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
1161 | + $getEntryFunc = 'firstEntry'; |
|
1162 | + if(($entries !== false) && ($entries > 0)) { |
|
1163 | + if(!is_null($maxF) && $entries > $maxEntries) { |
|
1164 | + $maxEntries = $entries; |
|
1165 | + $maxF = $filter; |
|
1166 | + } |
|
1167 | + $dnReadCount = 0; |
|
1168 | + do { |
|
1169 | + $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
1170 | + $getEntryFunc = 'nextEntry'; |
|
1171 | + if(!$this->ldap->isResource($entry)) { |
|
1172 | + continue 2; |
|
1173 | + } |
|
1174 | + $rr = $entry; //will be expected by nextEntry next round |
|
1175 | + $attributes = $this->ldap->getAttributes($cr, $entry); |
|
1176 | + $dn = $this->ldap->getDN($cr, $entry); |
|
1177 | + if($dn === false || in_array($dn, $dnRead)) { |
|
1178 | + continue; |
|
1179 | + } |
|
1180 | + $newItems = array(); |
|
1181 | + $state = $this->getAttributeValuesFromEntry($attributes, |
|
1182 | + $attr, |
|
1183 | + $newItems); |
|
1184 | + $dnReadCount++; |
|
1185 | + $foundItems = array_merge($foundItems, $newItems); |
|
1186 | + $this->resultCache[$dn][$attr] = $newItems; |
|
1187 | + $dnRead[] = $dn; |
|
1188 | + } while(($state === self::LRESULT_PROCESSED_SKIP |
|
1189 | + || $this->ldap->isResource($entry)) |
|
1190 | + && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
1191 | + } |
|
1192 | + } |
|
1193 | + |
|
1194 | + return array_unique($foundItems); |
|
1195 | + } |
|
1196 | + |
|
1197 | + /** |
|
1198 | + * determines if and which $attr are available on the LDAP server |
|
1199 | + * @param string[] $objectclasses the objectclasses to use as search filter |
|
1200 | + * @param string $attr the attribute to look for |
|
1201 | + * @param string $dbkey the dbkey of the setting the feature is connected to |
|
1202 | + * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
1203 | + * Configuration class |
|
1204 | + * @param bool $po whether the objectClass with most result entries |
|
1205 | + * shall be pre-selected via the result |
|
1206 | + * @return array|false list of found items. |
|
1207 | + * @throws \Exception |
|
1208 | + */ |
|
1209 | + private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
1210 | + $cr = $this->getConnection(); |
|
1211 | + if(!$cr) { |
|
1212 | + throw new \Exception('Could not connect to LDAP'); |
|
1213 | + } |
|
1214 | + $p = 'objectclass='; |
|
1215 | + foreach($objectclasses as $key => $value) { |
|
1216 | + $objectclasses[$key] = $p.$value; |
|
1217 | + } |
|
1218 | + $maxEntryObjC = ''; |
|
1219 | + |
|
1220 | + //how deep to dig? |
|
1221 | + //When looking for objectclasses, testing few entries is sufficient, |
|
1222 | + $dig = 3; |
|
1223 | + |
|
1224 | + $availableFeatures = |
|
1225 | + $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
1226 | + $dig, $maxEntryObjC); |
|
1227 | + if(is_array($availableFeatures) |
|
1228 | + && count($availableFeatures) > 0) { |
|
1229 | + natcasesort($availableFeatures); |
|
1230 | + //natcasesort keeps indices, but we must get rid of them for proper |
|
1231 | + //sorting in the web UI. Therefore: array_values |
|
1232 | + $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
1233 | + } else { |
|
1234 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
1235 | + } |
|
1236 | + |
|
1237 | + $setFeatures = $this->configuration->$confkey; |
|
1238 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
1239 | + //something is already configured? pre-select it. |
|
1240 | + $this->result->addChange($dbkey, $setFeatures); |
|
1241 | + } else if ($po && $maxEntryObjC !== '') { |
|
1242 | + //pre-select objectclass with most result entries |
|
1243 | + $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
1244 | + $this->applyFind($dbkey, $maxEntryObjC); |
|
1245 | + $this->result->addChange($dbkey, $maxEntryObjC); |
|
1246 | + } |
|
1247 | + |
|
1248 | + return $availableFeatures; |
|
1249 | + } |
|
1250 | + |
|
1251 | + /** |
|
1252 | + * appends a list of values fr |
|
1253 | + * @param resource $result the return value from ldap_get_attributes |
|
1254 | + * @param string $attribute the attribute values to look for |
|
1255 | + * @param array &$known new values will be appended here |
|
1256 | + * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
1257 | + * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
1258 | + */ |
|
1259 | + private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
1260 | + if(!is_array($result) |
|
1261 | + || !isset($result['count']) |
|
1262 | + || !$result['count'] > 0) { |
|
1263 | + return self::LRESULT_PROCESSED_INVALID; |
|
1264 | + } |
|
1265 | + |
|
1266 | + // strtolower on all keys for proper comparison |
|
1267 | + $result = \OCP\Util::mb_array_change_key_case($result); |
|
1268 | + $attribute = strtolower($attribute); |
|
1269 | + if(isset($result[$attribute])) { |
|
1270 | + foreach($result[$attribute] as $key => $val) { |
|
1271 | + if($key === 'count') { |
|
1272 | + continue; |
|
1273 | + } |
|
1274 | + if(!in_array($val, $known)) { |
|
1275 | + $known[] = $val; |
|
1276 | + } |
|
1277 | + } |
|
1278 | + return self::LRESULT_PROCESSED_OK; |
|
1279 | + } else { |
|
1280 | + return self::LRESULT_PROCESSED_SKIP; |
|
1281 | + } |
|
1282 | + } |
|
1283 | + |
|
1284 | + /** |
|
1285 | + * @return bool|mixed |
|
1286 | + */ |
|
1287 | + private function getConnection() { |
|
1288 | + if(!is_null($this->cr)) { |
|
1289 | + return $this->cr; |
|
1290 | + } |
|
1291 | + |
|
1292 | + $cr = $this->ldap->connect( |
|
1293 | + $this->configuration->ldapHost, |
|
1294 | + $this->configuration->ldapPort |
|
1295 | + ); |
|
1296 | + |
|
1297 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
1298 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
1299 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
1300 | + if($this->configuration->ldapTLS === 1) { |
|
1301 | + $this->ldap->startTls($cr); |
|
1302 | + } |
|
1303 | + |
|
1304 | + $lo = @$this->ldap->bind($cr, |
|
1305 | + $this->configuration->ldapAgentName, |
|
1306 | + $this->configuration->ldapAgentPassword); |
|
1307 | + if($lo === true) { |
|
1308 | + $this->$cr = $cr; |
|
1309 | + return $cr; |
|
1310 | + } |
|
1311 | + |
|
1312 | + return false; |
|
1313 | + } |
|
1314 | + |
|
1315 | + /** |
|
1316 | + * @return array |
|
1317 | + */ |
|
1318 | + private function getDefaultLdapPortSettings() { |
|
1319 | + static $settings = array( |
|
1320 | + array('port' => 7636, 'tls' => false), |
|
1321 | + array('port' => 636, 'tls' => false), |
|
1322 | + array('port' => 7389, 'tls' => true), |
|
1323 | + array('port' => 389, 'tls' => true), |
|
1324 | + array('port' => 7389, 'tls' => false), |
|
1325 | + array('port' => 389, 'tls' => false), |
|
1326 | + ); |
|
1327 | + return $settings; |
|
1328 | + } |
|
1329 | + |
|
1330 | + /** |
|
1331 | + * @return array |
|
1332 | + */ |
|
1333 | + private function getPortSettingsToTry() { |
|
1334 | + //389 ← LDAP / Unencrypted or StartTLS |
|
1335 | + //636 ← LDAPS / SSL |
|
1336 | + //7xxx ← UCS. need to be checked first, because both ports may be open |
|
1337 | + $host = $this->configuration->ldapHost; |
|
1338 | + $port = intval($this->configuration->ldapPort); |
|
1339 | + $portSettings = array(); |
|
1340 | + |
|
1341 | + //In case the port is already provided, we will check this first |
|
1342 | + if($port > 0) { |
|
1343 | + $hostInfo = parse_url($host); |
|
1344 | + if(!(is_array($hostInfo) |
|
1345 | + && isset($hostInfo['scheme']) |
|
1346 | + && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
1347 | + $portSettings[] = array('port' => $port, 'tls' => true); |
|
1348 | + } |
|
1349 | + $portSettings[] =array('port' => $port, 'tls' => false); |
|
1350 | + } |
|
1351 | + |
|
1352 | + //default ports |
|
1353 | + $portSettings = array_merge($portSettings, |
|
1354 | + $this->getDefaultLdapPortSettings()); |
|
1355 | + |
|
1356 | + return $portSettings; |
|
1357 | + } |
|
1358 | 1358 | |
1359 | 1359 | |
1360 | 1360 | } |
@@ -33,341 +33,341 @@ |
||
33 | 33 | use OCA\User_LDAP\Exceptions\ConstraintViolationException; |
34 | 34 | |
35 | 35 | class LDAP implements ILDAPWrapper { |
36 | - protected $curFunc = ''; |
|
37 | - protected $curArgs = array(); |
|
38 | - |
|
39 | - /** |
|
40 | - * @param resource $link |
|
41 | - * @param string $dn |
|
42 | - * @param string $password |
|
43 | - * @return bool|mixed |
|
44 | - */ |
|
45 | - public function bind($link, $dn, $password) { |
|
46 | - return $this->invokeLDAPMethod('bind', $link, $dn, $password); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @param string $host |
|
51 | - * @param string $port |
|
52 | - * @return mixed |
|
53 | - */ |
|
54 | - public function connect($host, $port) { |
|
55 | - if(strpos($host, '://') === false) { |
|
56 | - $host = 'ldap://' . $host; |
|
57 | - } |
|
58 | - if(strpos($host, ':', strpos($host, '://') + 1) === false) { |
|
59 | - //ldap_connect ignores port parameter when URLs are passed |
|
60 | - $host .= ':' . $port; |
|
61 | - } |
|
62 | - return $this->invokeLDAPMethod('connect', $host); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param LDAP $link |
|
67 | - * @param LDAP $result |
|
68 | - * @param string $cookie |
|
69 | - * @return bool|LDAP |
|
70 | - */ |
|
71 | - public function controlPagedResultResponse($link, $result, &$cookie) { |
|
72 | - $this->preFunctionCall('ldap_control_paged_result_response', |
|
73 | - array($link, $result, $cookie)); |
|
74 | - $result = ldap_control_paged_result_response($link, $result, $cookie); |
|
75 | - $this->postFunctionCall(); |
|
76 | - |
|
77 | - return $result; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param LDAP $link |
|
82 | - * @param int $pageSize |
|
83 | - * @param bool $isCritical |
|
84 | - * @param string $cookie |
|
85 | - * @return mixed|true |
|
86 | - */ |
|
87 | - public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
|
88 | - return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize, |
|
89 | - $isCritical, $cookie); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param LDAP $link |
|
94 | - * @param LDAP $result |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - public function countEntries($link, $result) { |
|
98 | - return $this->invokeLDAPMethod('count_entries', $link, $result); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param LDAP $link |
|
103 | - * @return mixed|string |
|
104 | - */ |
|
105 | - public function errno($link) { |
|
106 | - return $this->invokeLDAPMethod('errno', $link); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param LDAP $link |
|
111 | - * @return int|mixed |
|
112 | - */ |
|
113 | - public function error($link) { |
|
114 | - return $this->invokeLDAPMethod('error', $link); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Splits DN into its component parts |
|
119 | - * @param string $dn |
|
120 | - * @param int @withAttrib |
|
121 | - * @return array|false |
|
122 | - * @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
|
123 | - */ |
|
124 | - public function explodeDN($dn, $withAttrib) { |
|
125 | - return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param LDAP $link |
|
130 | - * @param LDAP $result |
|
131 | - * @return mixed |
|
132 | - */ |
|
133 | - public function firstEntry($link, $result) { |
|
134 | - return $this->invokeLDAPMethod('first_entry', $link, $result); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param LDAP $link |
|
139 | - * @param LDAP $result |
|
140 | - * @return array|mixed |
|
141 | - */ |
|
142 | - public function getAttributes($link, $result) { |
|
143 | - return $this->invokeLDAPMethod('get_attributes', $link, $result); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param LDAP $link |
|
148 | - * @param LDAP $result |
|
149 | - * @return mixed|string |
|
150 | - */ |
|
151 | - public function getDN($link, $result) { |
|
152 | - return $this->invokeLDAPMethod('get_dn', $link, $result); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @param LDAP $link |
|
157 | - * @param LDAP $result |
|
158 | - * @return array|mixed |
|
159 | - */ |
|
160 | - public function getEntries($link, $result) { |
|
161 | - return $this->invokeLDAPMethod('get_entries', $link, $result); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param LDAP $link |
|
166 | - * @param resource $result |
|
167 | - * @return mixed |
|
168 | - */ |
|
169 | - public function nextEntry($link, $result) { |
|
170 | - return $this->invokeLDAPMethod('next_entry', $link, $result); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param LDAP $link |
|
175 | - * @param string $baseDN |
|
176 | - * @param string $filter |
|
177 | - * @param array $attr |
|
178 | - * @return mixed |
|
179 | - */ |
|
180 | - public function read($link, $baseDN, $filter, $attr) { |
|
181 | - return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @param LDAP $link |
|
186 | - * @param string $baseDN |
|
187 | - * @param string $filter |
|
188 | - * @param array $attr |
|
189 | - * @param int $attrsOnly |
|
190 | - * @param int $limit |
|
191 | - * @return mixed |
|
192 | - */ |
|
193 | - public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
|
194 | - return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param LDAP $link |
|
199 | - * @param string $userDN |
|
200 | - * @param string $password |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function modReplace($link, $userDN, $password) { |
|
204 | - return $this->invokeLDAPMethod('mod_replace', $link, $userDN, array('userPassword' => $password)); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @param LDAP $link |
|
209 | - * @param string $option |
|
210 | - * @param int $value |
|
211 | - * @return bool|mixed |
|
212 | - */ |
|
213 | - public function setOption($link, $option, $value) { |
|
214 | - return $this->invokeLDAPMethod('set_option', $link, $option, $value); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @param LDAP $link |
|
219 | - * @return mixed|true |
|
220 | - */ |
|
221 | - public function startTls($link) { |
|
222 | - return $this->invokeLDAPMethod('start_tls', $link); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * @param resource $link |
|
227 | - * @return bool|mixed |
|
228 | - */ |
|
229 | - public function unbind($link) { |
|
230 | - return $this->invokeLDAPMethod('unbind', $link); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Checks whether the server supports LDAP |
|
235 | - * @return boolean if it the case, false otherwise |
|
236 | - * */ |
|
237 | - public function areLDAPFunctionsAvailable() { |
|
238 | - return function_exists('ldap_connect'); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Checks whether PHP supports LDAP Paged Results |
|
243 | - * @return boolean if it the case, false otherwise |
|
244 | - * */ |
|
245 | - public function hasPagedResultSupport() { |
|
246 | - $hasSupport = function_exists('ldap_control_paged_result') |
|
247 | - && function_exists('ldap_control_paged_result_response'); |
|
248 | - return $hasSupport; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Checks whether the submitted parameter is a resource |
|
253 | - * @param Resource $resource the resource variable to check |
|
254 | - * @return bool true if it is a resource, false otherwise |
|
255 | - */ |
|
256 | - public function isResource($resource) { |
|
257 | - return is_resource($resource); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Checks whether the return value from LDAP is wrong or not. |
|
262 | - * |
|
263 | - * When using ldap_search we provide an array, in case multiple bases are |
|
264 | - * configured. Thus, we need to check the array elements. |
|
265 | - * |
|
266 | - * @param $result |
|
267 | - * @return bool |
|
268 | - */ |
|
269 | - protected function isResultFalse($result) { |
|
270 | - if($result === false) { |
|
271 | - return true; |
|
272 | - } |
|
273 | - |
|
274 | - if($this->curFunc === 'ldap_search' && is_array($result)) { |
|
275 | - foreach ($result as $singleResult) { |
|
276 | - if($singleResult === false) { |
|
277 | - return true; |
|
278 | - } |
|
279 | - } |
|
280 | - } |
|
281 | - |
|
282 | - return false; |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @return mixed |
|
287 | - */ |
|
288 | - protected function invokeLDAPMethod() { |
|
289 | - $arguments = func_get_args(); |
|
290 | - $func = 'ldap_' . array_shift($arguments); |
|
291 | - if(function_exists($func)) { |
|
292 | - $this->preFunctionCall($func, $arguments); |
|
293 | - $result = call_user_func_array($func, $arguments); |
|
294 | - if ($this->isResultFalse($result)) { |
|
295 | - $this->postFunctionCall(); |
|
296 | - } |
|
297 | - return $result; |
|
298 | - } |
|
299 | - return null; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * @param string $functionName |
|
304 | - * @param array $args |
|
305 | - */ |
|
306 | - private function preFunctionCall($functionName, $args) { |
|
307 | - $this->curFunc = $functionName; |
|
308 | - $this->curArgs = $args; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Analyzes the returned LDAP error and acts accordingly if not 0 |
|
313 | - * |
|
314 | - * @param resource $resource the LDAP Connection resource |
|
315 | - * @throws ConstraintViolationException |
|
316 | - * @throws ServerNotAvailableException |
|
317 | - * @throws \Exception |
|
318 | - */ |
|
319 | - private function processLDAPError($resource) { |
|
320 | - $errorCode = ldap_errno($resource); |
|
321 | - if($errorCode === 0) { |
|
322 | - return; |
|
323 | - } |
|
324 | - $errorMsg = ldap_error($resource); |
|
325 | - |
|
326 | - if($this->curFunc === 'ldap_get_entries' |
|
327 | - && $errorCode === -4) { |
|
328 | - } else if ($errorCode === 32) { |
|
329 | - //for now |
|
330 | - } else if ($errorCode === 10) { |
|
331 | - //referrals, we switch them off, but then there is AD :) |
|
332 | - } else if ($errorCode === -1) { |
|
333 | - throw new ServerNotAvailableException('Lost connection to LDAP server.'); |
|
334 | - } else if ($errorCode === 48) { |
|
335 | - throw new \Exception('LDAP authentication method rejected', $errorCode); |
|
336 | - } else if ($errorCode === 1) { |
|
337 | - throw new \Exception('LDAP Operations error', $errorCode); |
|
338 | - } else if ($errorCode === 19) { |
|
339 | - ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error); |
|
340 | - throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode); |
|
341 | - } else { |
|
342 | - \OCP\Util::writeLog('user_ldap', |
|
343 | - 'LDAP error '.$errorMsg.' (' . |
|
344 | - $errorCode.') after calling '. |
|
345 | - $this->curFunc, |
|
346 | - \OCP\Util::DEBUG); |
|
347 | - } |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Called after an ldap method is run to act on LDAP error if necessary |
|
352 | - */ |
|
353 | - private function postFunctionCall() { |
|
354 | - if($this->isResource($this->curArgs[0])) { |
|
355 | - $resource = $this->curArgs[0]; |
|
356 | - } else if( |
|
357 | - $this->curFunc === 'ldap_search' |
|
358 | - && is_array($this->curArgs[0]) |
|
359 | - && $this->isResource($this->curArgs[0][0]) |
|
360 | - ) { |
|
361 | - // we use always the same LDAP connection resource, is enough to |
|
362 | - // take the first one. |
|
363 | - $resource = $this->curArgs[0][0]; |
|
364 | - } else { |
|
365 | - return; |
|
366 | - } |
|
367 | - |
|
368 | - $this->processLDAPError($resource); |
|
369 | - |
|
370 | - $this->curFunc = ''; |
|
371 | - $this->curArgs = []; |
|
372 | - } |
|
36 | + protected $curFunc = ''; |
|
37 | + protected $curArgs = array(); |
|
38 | + |
|
39 | + /** |
|
40 | + * @param resource $link |
|
41 | + * @param string $dn |
|
42 | + * @param string $password |
|
43 | + * @return bool|mixed |
|
44 | + */ |
|
45 | + public function bind($link, $dn, $password) { |
|
46 | + return $this->invokeLDAPMethod('bind', $link, $dn, $password); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @param string $host |
|
51 | + * @param string $port |
|
52 | + * @return mixed |
|
53 | + */ |
|
54 | + public function connect($host, $port) { |
|
55 | + if(strpos($host, '://') === false) { |
|
56 | + $host = 'ldap://' . $host; |
|
57 | + } |
|
58 | + if(strpos($host, ':', strpos($host, '://') + 1) === false) { |
|
59 | + //ldap_connect ignores port parameter when URLs are passed |
|
60 | + $host .= ':' . $port; |
|
61 | + } |
|
62 | + return $this->invokeLDAPMethod('connect', $host); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param LDAP $link |
|
67 | + * @param LDAP $result |
|
68 | + * @param string $cookie |
|
69 | + * @return bool|LDAP |
|
70 | + */ |
|
71 | + public function controlPagedResultResponse($link, $result, &$cookie) { |
|
72 | + $this->preFunctionCall('ldap_control_paged_result_response', |
|
73 | + array($link, $result, $cookie)); |
|
74 | + $result = ldap_control_paged_result_response($link, $result, $cookie); |
|
75 | + $this->postFunctionCall(); |
|
76 | + |
|
77 | + return $result; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param LDAP $link |
|
82 | + * @param int $pageSize |
|
83 | + * @param bool $isCritical |
|
84 | + * @param string $cookie |
|
85 | + * @return mixed|true |
|
86 | + */ |
|
87 | + public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
|
88 | + return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize, |
|
89 | + $isCritical, $cookie); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param LDAP $link |
|
94 | + * @param LDAP $result |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + public function countEntries($link, $result) { |
|
98 | + return $this->invokeLDAPMethod('count_entries', $link, $result); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param LDAP $link |
|
103 | + * @return mixed|string |
|
104 | + */ |
|
105 | + public function errno($link) { |
|
106 | + return $this->invokeLDAPMethod('errno', $link); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param LDAP $link |
|
111 | + * @return int|mixed |
|
112 | + */ |
|
113 | + public function error($link) { |
|
114 | + return $this->invokeLDAPMethod('error', $link); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Splits DN into its component parts |
|
119 | + * @param string $dn |
|
120 | + * @param int @withAttrib |
|
121 | + * @return array|false |
|
122 | + * @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
|
123 | + */ |
|
124 | + public function explodeDN($dn, $withAttrib) { |
|
125 | + return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param LDAP $link |
|
130 | + * @param LDAP $result |
|
131 | + * @return mixed |
|
132 | + */ |
|
133 | + public function firstEntry($link, $result) { |
|
134 | + return $this->invokeLDAPMethod('first_entry', $link, $result); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param LDAP $link |
|
139 | + * @param LDAP $result |
|
140 | + * @return array|mixed |
|
141 | + */ |
|
142 | + public function getAttributes($link, $result) { |
|
143 | + return $this->invokeLDAPMethod('get_attributes', $link, $result); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param LDAP $link |
|
148 | + * @param LDAP $result |
|
149 | + * @return mixed|string |
|
150 | + */ |
|
151 | + public function getDN($link, $result) { |
|
152 | + return $this->invokeLDAPMethod('get_dn', $link, $result); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @param LDAP $link |
|
157 | + * @param LDAP $result |
|
158 | + * @return array|mixed |
|
159 | + */ |
|
160 | + public function getEntries($link, $result) { |
|
161 | + return $this->invokeLDAPMethod('get_entries', $link, $result); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param LDAP $link |
|
166 | + * @param resource $result |
|
167 | + * @return mixed |
|
168 | + */ |
|
169 | + public function nextEntry($link, $result) { |
|
170 | + return $this->invokeLDAPMethod('next_entry', $link, $result); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param LDAP $link |
|
175 | + * @param string $baseDN |
|
176 | + * @param string $filter |
|
177 | + * @param array $attr |
|
178 | + * @return mixed |
|
179 | + */ |
|
180 | + public function read($link, $baseDN, $filter, $attr) { |
|
181 | + return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @param LDAP $link |
|
186 | + * @param string $baseDN |
|
187 | + * @param string $filter |
|
188 | + * @param array $attr |
|
189 | + * @param int $attrsOnly |
|
190 | + * @param int $limit |
|
191 | + * @return mixed |
|
192 | + */ |
|
193 | + public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
|
194 | + return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param LDAP $link |
|
199 | + * @param string $userDN |
|
200 | + * @param string $password |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function modReplace($link, $userDN, $password) { |
|
204 | + return $this->invokeLDAPMethod('mod_replace', $link, $userDN, array('userPassword' => $password)); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @param LDAP $link |
|
209 | + * @param string $option |
|
210 | + * @param int $value |
|
211 | + * @return bool|mixed |
|
212 | + */ |
|
213 | + public function setOption($link, $option, $value) { |
|
214 | + return $this->invokeLDAPMethod('set_option', $link, $option, $value); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @param LDAP $link |
|
219 | + * @return mixed|true |
|
220 | + */ |
|
221 | + public function startTls($link) { |
|
222 | + return $this->invokeLDAPMethod('start_tls', $link); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * @param resource $link |
|
227 | + * @return bool|mixed |
|
228 | + */ |
|
229 | + public function unbind($link) { |
|
230 | + return $this->invokeLDAPMethod('unbind', $link); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Checks whether the server supports LDAP |
|
235 | + * @return boolean if it the case, false otherwise |
|
236 | + * */ |
|
237 | + public function areLDAPFunctionsAvailable() { |
|
238 | + return function_exists('ldap_connect'); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Checks whether PHP supports LDAP Paged Results |
|
243 | + * @return boolean if it the case, false otherwise |
|
244 | + * */ |
|
245 | + public function hasPagedResultSupport() { |
|
246 | + $hasSupport = function_exists('ldap_control_paged_result') |
|
247 | + && function_exists('ldap_control_paged_result_response'); |
|
248 | + return $hasSupport; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Checks whether the submitted parameter is a resource |
|
253 | + * @param Resource $resource the resource variable to check |
|
254 | + * @return bool true if it is a resource, false otherwise |
|
255 | + */ |
|
256 | + public function isResource($resource) { |
|
257 | + return is_resource($resource); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Checks whether the return value from LDAP is wrong or not. |
|
262 | + * |
|
263 | + * When using ldap_search we provide an array, in case multiple bases are |
|
264 | + * configured. Thus, we need to check the array elements. |
|
265 | + * |
|
266 | + * @param $result |
|
267 | + * @return bool |
|
268 | + */ |
|
269 | + protected function isResultFalse($result) { |
|
270 | + if($result === false) { |
|
271 | + return true; |
|
272 | + } |
|
273 | + |
|
274 | + if($this->curFunc === 'ldap_search' && is_array($result)) { |
|
275 | + foreach ($result as $singleResult) { |
|
276 | + if($singleResult === false) { |
|
277 | + return true; |
|
278 | + } |
|
279 | + } |
|
280 | + } |
|
281 | + |
|
282 | + return false; |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @return mixed |
|
287 | + */ |
|
288 | + protected function invokeLDAPMethod() { |
|
289 | + $arguments = func_get_args(); |
|
290 | + $func = 'ldap_' . array_shift($arguments); |
|
291 | + if(function_exists($func)) { |
|
292 | + $this->preFunctionCall($func, $arguments); |
|
293 | + $result = call_user_func_array($func, $arguments); |
|
294 | + if ($this->isResultFalse($result)) { |
|
295 | + $this->postFunctionCall(); |
|
296 | + } |
|
297 | + return $result; |
|
298 | + } |
|
299 | + return null; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * @param string $functionName |
|
304 | + * @param array $args |
|
305 | + */ |
|
306 | + private function preFunctionCall($functionName, $args) { |
|
307 | + $this->curFunc = $functionName; |
|
308 | + $this->curArgs = $args; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Analyzes the returned LDAP error and acts accordingly if not 0 |
|
313 | + * |
|
314 | + * @param resource $resource the LDAP Connection resource |
|
315 | + * @throws ConstraintViolationException |
|
316 | + * @throws ServerNotAvailableException |
|
317 | + * @throws \Exception |
|
318 | + */ |
|
319 | + private function processLDAPError($resource) { |
|
320 | + $errorCode = ldap_errno($resource); |
|
321 | + if($errorCode === 0) { |
|
322 | + return; |
|
323 | + } |
|
324 | + $errorMsg = ldap_error($resource); |
|
325 | + |
|
326 | + if($this->curFunc === 'ldap_get_entries' |
|
327 | + && $errorCode === -4) { |
|
328 | + } else if ($errorCode === 32) { |
|
329 | + //for now |
|
330 | + } else if ($errorCode === 10) { |
|
331 | + //referrals, we switch them off, but then there is AD :) |
|
332 | + } else if ($errorCode === -1) { |
|
333 | + throw new ServerNotAvailableException('Lost connection to LDAP server.'); |
|
334 | + } else if ($errorCode === 48) { |
|
335 | + throw new \Exception('LDAP authentication method rejected', $errorCode); |
|
336 | + } else if ($errorCode === 1) { |
|
337 | + throw new \Exception('LDAP Operations error', $errorCode); |
|
338 | + } else if ($errorCode === 19) { |
|
339 | + ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error); |
|
340 | + throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode); |
|
341 | + } else { |
|
342 | + \OCP\Util::writeLog('user_ldap', |
|
343 | + 'LDAP error '.$errorMsg.' (' . |
|
344 | + $errorCode.') after calling '. |
|
345 | + $this->curFunc, |
|
346 | + \OCP\Util::DEBUG); |
|
347 | + } |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Called after an ldap method is run to act on LDAP error if necessary |
|
352 | + */ |
|
353 | + private function postFunctionCall() { |
|
354 | + if($this->isResource($this->curArgs[0])) { |
|
355 | + $resource = $this->curArgs[0]; |
|
356 | + } else if( |
|
357 | + $this->curFunc === 'ldap_search' |
|
358 | + && is_array($this->curArgs[0]) |
|
359 | + && $this->isResource($this->curArgs[0][0]) |
|
360 | + ) { |
|
361 | + // we use always the same LDAP connection resource, is enough to |
|
362 | + // take the first one. |
|
363 | + $resource = $this->curArgs[0][0]; |
|
364 | + } else { |
|
365 | + return; |
|
366 | + } |
|
367 | + |
|
368 | + $this->processLDAPError($resource); |
|
369 | + |
|
370 | + $this->curFunc = ''; |
|
371 | + $this->curArgs = []; |
|
372 | + } |
|
373 | 373 | } |
@@ -31,84 +31,84 @@ |
||
31 | 31 | * @package OCA\User_LDAP |
32 | 32 | */ |
33 | 33 | class DeletedUsersIndex { |
34 | - /** |
|
35 | - * @var \OCP\IConfig $config |
|
36 | - */ |
|
37 | - protected $config; |
|
34 | + /** |
|
35 | + * @var \OCP\IConfig $config |
|
36 | + */ |
|
37 | + protected $config; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var \OCP\IDBConnection $db |
|
41 | - */ |
|
42 | - protected $db; |
|
39 | + /** |
|
40 | + * @var \OCP\IDBConnection $db |
|
41 | + */ |
|
42 | + protected $db; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @var \OCA\User_LDAP\Mapping\UserMapping $mapping |
|
46 | - */ |
|
47 | - protected $mapping; |
|
44 | + /** |
|
45 | + * @var \OCA\User_LDAP\Mapping\UserMapping $mapping |
|
46 | + */ |
|
47 | + protected $mapping; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @var array $deletedUsers |
|
51 | - */ |
|
52 | - protected $deletedUsers; |
|
49 | + /** |
|
50 | + * @var array $deletedUsers |
|
51 | + */ |
|
52 | + protected $deletedUsers; |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param \OCP\IConfig $config |
|
56 | - * @param \OCP\IDBConnection $db |
|
57 | - * @param \OCA\User_LDAP\Mapping\UserMapping $mapping |
|
58 | - */ |
|
59 | - public function __construct(\OCP\IConfig $config, \OCP\IDBConnection $db, UserMapping $mapping) { |
|
60 | - $this->config = $config; |
|
61 | - $this->db = $db; |
|
62 | - $this->mapping = $mapping; |
|
63 | - } |
|
54 | + /** |
|
55 | + * @param \OCP\IConfig $config |
|
56 | + * @param \OCP\IDBConnection $db |
|
57 | + * @param \OCA\User_LDAP\Mapping\UserMapping $mapping |
|
58 | + */ |
|
59 | + public function __construct(\OCP\IConfig $config, \OCP\IDBConnection $db, UserMapping $mapping) { |
|
60 | + $this->config = $config; |
|
61 | + $this->db = $db; |
|
62 | + $this->mapping = $mapping; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * reads LDAP users marked as deleted from the database |
|
67 | - * @return \OCA\User_LDAP\User\OfflineUser[] |
|
68 | - */ |
|
69 | - private function fetchDeletedUsers() { |
|
70 | - $deletedUsers = $this->config->getUsersForUserValue( |
|
71 | - 'user_ldap', 'isDeleted', '1'); |
|
65 | + /** |
|
66 | + * reads LDAP users marked as deleted from the database |
|
67 | + * @return \OCA\User_LDAP\User\OfflineUser[] |
|
68 | + */ |
|
69 | + private function fetchDeletedUsers() { |
|
70 | + $deletedUsers = $this->config->getUsersForUserValue( |
|
71 | + 'user_ldap', 'isDeleted', '1'); |
|
72 | 72 | |
73 | - $userObjects = array(); |
|
74 | - foreach($deletedUsers as $user) { |
|
75 | - $userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping); |
|
76 | - } |
|
77 | - $this->deletedUsers = $userObjects; |
|
73 | + $userObjects = array(); |
|
74 | + foreach($deletedUsers as $user) { |
|
75 | + $userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping); |
|
76 | + } |
|
77 | + $this->deletedUsers = $userObjects; |
|
78 | 78 | |
79 | - return $this->deletedUsers; |
|
80 | - } |
|
79 | + return $this->deletedUsers; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * returns all LDAP users that are marked as deleted |
|
84 | - * @return \OCA\User_LDAP\User\OfflineUser[] |
|
85 | - */ |
|
86 | - public function getUsers() { |
|
87 | - if(is_array($this->deletedUsers)) { |
|
88 | - return $this->deletedUsers; |
|
89 | - } |
|
90 | - return $this->fetchDeletedUsers(); |
|
91 | - } |
|
82 | + /** |
|
83 | + * returns all LDAP users that are marked as deleted |
|
84 | + * @return \OCA\User_LDAP\User\OfflineUser[] |
|
85 | + */ |
|
86 | + public function getUsers() { |
|
87 | + if(is_array($this->deletedUsers)) { |
|
88 | + return $this->deletedUsers; |
|
89 | + } |
|
90 | + return $this->fetchDeletedUsers(); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * whether at least one user was detected as deleted |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - public function hasUsers() { |
|
98 | - if($this->deletedUsers === false) { |
|
99 | - $this->fetchDeletedUsers(); |
|
100 | - } |
|
101 | - if(is_array($this->deletedUsers) && count($this->deletedUsers) > 0) { |
|
102 | - return true; |
|
103 | - } |
|
104 | - return false; |
|
105 | - } |
|
93 | + /** |
|
94 | + * whether at least one user was detected as deleted |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + public function hasUsers() { |
|
98 | + if($this->deletedUsers === false) { |
|
99 | + $this->fetchDeletedUsers(); |
|
100 | + } |
|
101 | + if(is_array($this->deletedUsers) && count($this->deletedUsers) > 0) { |
|
102 | + return true; |
|
103 | + } |
|
104 | + return false; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * marks a user as deleted |
|
109 | - * @param string $ocName |
|
110 | - */ |
|
111 | - public function markUser($ocName) { |
|
112 | - $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1'); |
|
113 | - } |
|
107 | + /** |
|
108 | + * marks a user as deleted |
|
109 | + * @param string $ocName |
|
110 | + */ |
|
111 | + public function markUser($ocName) { |
|
112 | + $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1'); |
|
113 | + } |
|
114 | 114 | } |
@@ -30,13 +30,13 @@ |
||
30 | 30 | * defines methods that are required by User class for LDAP interaction |
31 | 31 | */ |
32 | 32 | interface IUserTools { |
33 | - public function getConnection(); |
|
33 | + public function getConnection(); |
|
34 | 34 | |
35 | - public function readAttribute($dn, $attr, $filter = 'objectClass=*'); |
|
35 | + public function readAttribute($dn, $attr, $filter = 'objectClass=*'); |
|
36 | 36 | |
37 | - public function stringResemblesDN($string); |
|
37 | + public function stringResemblesDN($string); |
|
38 | 38 | |
39 | - public function dn2username($dn, $ldapname = null); |
|
39 | + public function dn2username($dn, $ldapname = null); |
|
40 | 40 | |
41 | - public function username2dn($name); |
|
41 | + public function username2dn($name); |
|
42 | 42 | } |
@@ -42,200 +42,200 @@ |
||
42 | 42 | * cache |
43 | 43 | */ |
44 | 44 | class Manager { |
45 | - /** @var IUserTools */ |
|
46 | - protected $access; |
|
47 | - |
|
48 | - /** @var IConfig */ |
|
49 | - protected $ocConfig; |
|
50 | - |
|
51 | - /** @var IDBConnection */ |
|
52 | - protected $db; |
|
53 | - |
|
54 | - /** @var FilesystemHelper */ |
|
55 | - protected $ocFilesystem; |
|
56 | - |
|
57 | - /** @var LogWrapper */ |
|
58 | - protected $ocLog; |
|
59 | - |
|
60 | - /** @var Image */ |
|
61 | - protected $image; |
|
62 | - |
|
63 | - /** @param \OCP\IAvatarManager */ |
|
64 | - protected $avatarManager; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var CappedMemoryCache $usersByDN |
|
68 | - */ |
|
69 | - protected $usersByDN; |
|
70 | - /** |
|
71 | - * @var CappedMemoryCache $usersByUid |
|
72 | - */ |
|
73 | - protected $usersByUid; |
|
74 | - |
|
75 | - /** |
|
76 | - * @param IConfig $ocConfig |
|
77 | - * @param \OCA\User_LDAP\FilesystemHelper $ocFilesystem object that |
|
78 | - * gives access to necessary functions from the OC filesystem |
|
79 | - * @param \OCA\User_LDAP\LogWrapper $ocLog |
|
80 | - * @param IAvatarManager $avatarManager |
|
81 | - * @param Image $image an empty image instance |
|
82 | - * @param IDBConnection $db |
|
83 | - * @throws \Exception when the methods mentioned above do not exist |
|
84 | - */ |
|
85 | - public function __construct(IConfig $ocConfig, |
|
86 | - FilesystemHelper $ocFilesystem, LogWrapper $ocLog, |
|
87 | - IAvatarManager $avatarManager, Image $image, |
|
88 | - IDBConnection $db, IUserManager $userManager) { |
|
89 | - |
|
90 | - $this->ocConfig = $ocConfig; |
|
91 | - $this->ocFilesystem = $ocFilesystem; |
|
92 | - $this->ocLog = $ocLog; |
|
93 | - $this->avatarManager = $avatarManager; |
|
94 | - $this->image = $image; |
|
95 | - $this->db = $db; |
|
96 | - $this->userManager = $userManager; |
|
97 | - $this->usersByDN = new CappedMemoryCache(); |
|
98 | - $this->usersByUid = new CappedMemoryCache(); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @brief binds manager to an instance of IUserTools (implemented by |
|
103 | - * Access). It needs to be assigned first before the manager can be used. |
|
104 | - * @param IUserTools |
|
105 | - */ |
|
106 | - public function setLdapAccess(IUserTools $access) { |
|
107 | - $this->access = $access; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @brief creates an instance of User and caches (just runtime) it in the |
|
112 | - * property array |
|
113 | - * @param string $dn the DN of the user |
|
114 | - * @param string $uid the internal (owncloud) username |
|
115 | - * @return \OCA\User_LDAP\User\User |
|
116 | - */ |
|
117 | - private function createAndCache($dn, $uid) { |
|
118 | - $this->checkAccess(); |
|
119 | - $user = new User($uid, $dn, $this->access, $this->ocConfig, |
|
120 | - $this->ocFilesystem, clone $this->image, $this->ocLog, |
|
121 | - $this->avatarManager, $this->userManager); |
|
122 | - $this->usersByDN[$dn] = $user; |
|
123 | - $this->usersByUid[$uid] = $user; |
|
124 | - return $user; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @brief checks whether the Access instance has been set |
|
129 | - * @throws \Exception if Access has not been set |
|
130 | - * @return null |
|
131 | - */ |
|
132 | - private function checkAccess() { |
|
133 | - if(is_null($this->access)) { |
|
134 | - throw new \Exception('LDAP Access instance must be set first'); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * returns a list of attributes that will be processed further, e.g. quota, |
|
140 | - * email, displayname, or others. |
|
141 | - * @param bool $minimal - optional, set to true to skip attributes with big |
|
142 | - * payload |
|
143 | - * @return string[] |
|
144 | - */ |
|
145 | - public function getAttributes($minimal = false) { |
|
146 | - $attributes = array('dn', 'uid', 'samaccountname', 'memberof'); |
|
147 | - $possible = array( |
|
148 | - $this->access->getConnection()->ldapQuotaAttribute, |
|
149 | - $this->access->getConnection()->ldapEmailAttribute, |
|
150 | - $this->access->getConnection()->ldapUserDisplayName, |
|
151 | - $this->access->getConnection()->ldapUserDisplayName2, |
|
152 | - ); |
|
153 | - foreach($possible as $attr) { |
|
154 | - if(!is_null($attr)) { |
|
155 | - $attributes[] = $attr; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - $homeRule = $this->access->getConnection()->homeFolderNamingRule; |
|
160 | - if(strpos($homeRule, 'attr:') === 0) { |
|
161 | - $attributes[] = substr($homeRule, strlen('attr:')); |
|
162 | - } |
|
163 | - |
|
164 | - if(!$minimal) { |
|
165 | - // attributes that are not really important but may come with big |
|
166 | - // payload. |
|
167 | - $attributes = array_merge($attributes, array( |
|
168 | - 'jpegphoto', |
|
169 | - 'thumbnailphoto' |
|
170 | - )); |
|
171 | - } |
|
172 | - |
|
173 | - return $attributes; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Checks whether the specified user is marked as deleted |
|
178 | - * @param string $id the ownCloud user name |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function isDeletedUser($id) { |
|
182 | - $isDeleted = $this->ocConfig->getUserValue( |
|
183 | - $id, 'user_ldap', 'isDeleted', 0); |
|
184 | - return intval($isDeleted) === 1; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * creates and returns an instance of OfflineUser for the specified user |
|
189 | - * @param string $id |
|
190 | - * @return \OCA\User_LDAP\User\OfflineUser |
|
191 | - */ |
|
192 | - public function getDeletedUser($id) { |
|
193 | - return new OfflineUser( |
|
194 | - $id, |
|
195 | - $this->ocConfig, |
|
196 | - $this->db, |
|
197 | - $this->access->getUserMapper()); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * @brief returns a User object by it's ownCloud username |
|
202 | - * @param string $id the DN or username of the user |
|
203 | - * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null |
|
204 | - */ |
|
205 | - protected function createInstancyByUserName($id) { |
|
206 | - //most likely a uid. Check whether it is a deleted user |
|
207 | - if($this->isDeletedUser($id)) { |
|
208 | - return $this->getDeletedUser($id); |
|
209 | - } |
|
210 | - $dn = $this->access->username2dn($id); |
|
211 | - if($dn !== false) { |
|
212 | - return $this->createAndCache($dn, $id); |
|
213 | - } |
|
214 | - return null; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @brief returns a User object by it's DN or ownCloud username |
|
219 | - * @param string $id the DN or username of the user |
|
220 | - * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null |
|
221 | - * @throws \Exception when connection could not be established |
|
222 | - */ |
|
223 | - public function get($id) { |
|
224 | - $this->checkAccess(); |
|
225 | - if(isset($this->usersByDN[$id])) { |
|
226 | - return $this->usersByDN[$id]; |
|
227 | - } else if(isset($this->usersByUid[$id])) { |
|
228 | - return $this->usersByUid[$id]; |
|
229 | - } |
|
230 | - |
|
231 | - if($this->access->stringResemblesDN($id) ) { |
|
232 | - $uid = $this->access->dn2username($id); |
|
233 | - if($uid !== false) { |
|
234 | - return $this->createAndCache($id, $uid); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - return $this->createInstancyByUserName($id); |
|
239 | - } |
|
45 | + /** @var IUserTools */ |
|
46 | + protected $access; |
|
47 | + |
|
48 | + /** @var IConfig */ |
|
49 | + protected $ocConfig; |
|
50 | + |
|
51 | + /** @var IDBConnection */ |
|
52 | + protected $db; |
|
53 | + |
|
54 | + /** @var FilesystemHelper */ |
|
55 | + protected $ocFilesystem; |
|
56 | + |
|
57 | + /** @var LogWrapper */ |
|
58 | + protected $ocLog; |
|
59 | + |
|
60 | + /** @var Image */ |
|
61 | + protected $image; |
|
62 | + |
|
63 | + /** @param \OCP\IAvatarManager */ |
|
64 | + protected $avatarManager; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var CappedMemoryCache $usersByDN |
|
68 | + */ |
|
69 | + protected $usersByDN; |
|
70 | + /** |
|
71 | + * @var CappedMemoryCache $usersByUid |
|
72 | + */ |
|
73 | + protected $usersByUid; |
|
74 | + |
|
75 | + /** |
|
76 | + * @param IConfig $ocConfig |
|
77 | + * @param \OCA\User_LDAP\FilesystemHelper $ocFilesystem object that |
|
78 | + * gives access to necessary functions from the OC filesystem |
|
79 | + * @param \OCA\User_LDAP\LogWrapper $ocLog |
|
80 | + * @param IAvatarManager $avatarManager |
|
81 | + * @param Image $image an empty image instance |
|
82 | + * @param IDBConnection $db |
|
83 | + * @throws \Exception when the methods mentioned above do not exist |
|
84 | + */ |
|
85 | + public function __construct(IConfig $ocConfig, |
|
86 | + FilesystemHelper $ocFilesystem, LogWrapper $ocLog, |
|
87 | + IAvatarManager $avatarManager, Image $image, |
|
88 | + IDBConnection $db, IUserManager $userManager) { |
|
89 | + |
|
90 | + $this->ocConfig = $ocConfig; |
|
91 | + $this->ocFilesystem = $ocFilesystem; |
|
92 | + $this->ocLog = $ocLog; |
|
93 | + $this->avatarManager = $avatarManager; |
|
94 | + $this->image = $image; |
|
95 | + $this->db = $db; |
|
96 | + $this->userManager = $userManager; |
|
97 | + $this->usersByDN = new CappedMemoryCache(); |
|
98 | + $this->usersByUid = new CappedMemoryCache(); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @brief binds manager to an instance of IUserTools (implemented by |
|
103 | + * Access). It needs to be assigned first before the manager can be used. |
|
104 | + * @param IUserTools |
|
105 | + */ |
|
106 | + public function setLdapAccess(IUserTools $access) { |
|
107 | + $this->access = $access; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @brief creates an instance of User and caches (just runtime) it in the |
|
112 | + * property array |
|
113 | + * @param string $dn the DN of the user |
|
114 | + * @param string $uid the internal (owncloud) username |
|
115 | + * @return \OCA\User_LDAP\User\User |
|
116 | + */ |
|
117 | + private function createAndCache($dn, $uid) { |
|
118 | + $this->checkAccess(); |
|
119 | + $user = new User($uid, $dn, $this->access, $this->ocConfig, |
|
120 | + $this->ocFilesystem, clone $this->image, $this->ocLog, |
|
121 | + $this->avatarManager, $this->userManager); |
|
122 | + $this->usersByDN[$dn] = $user; |
|
123 | + $this->usersByUid[$uid] = $user; |
|
124 | + return $user; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @brief checks whether the Access instance has been set |
|
129 | + * @throws \Exception if Access has not been set |
|
130 | + * @return null |
|
131 | + */ |
|
132 | + private function checkAccess() { |
|
133 | + if(is_null($this->access)) { |
|
134 | + throw new \Exception('LDAP Access instance must be set first'); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * returns a list of attributes that will be processed further, e.g. quota, |
|
140 | + * email, displayname, or others. |
|
141 | + * @param bool $minimal - optional, set to true to skip attributes with big |
|
142 | + * payload |
|
143 | + * @return string[] |
|
144 | + */ |
|
145 | + public function getAttributes($minimal = false) { |
|
146 | + $attributes = array('dn', 'uid', 'samaccountname', 'memberof'); |
|
147 | + $possible = array( |
|
148 | + $this->access->getConnection()->ldapQuotaAttribute, |
|
149 | + $this->access->getConnection()->ldapEmailAttribute, |
|
150 | + $this->access->getConnection()->ldapUserDisplayName, |
|
151 | + $this->access->getConnection()->ldapUserDisplayName2, |
|
152 | + ); |
|
153 | + foreach($possible as $attr) { |
|
154 | + if(!is_null($attr)) { |
|
155 | + $attributes[] = $attr; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + $homeRule = $this->access->getConnection()->homeFolderNamingRule; |
|
160 | + if(strpos($homeRule, 'attr:') === 0) { |
|
161 | + $attributes[] = substr($homeRule, strlen('attr:')); |
|
162 | + } |
|
163 | + |
|
164 | + if(!$minimal) { |
|
165 | + // attributes that are not really important but may come with big |
|
166 | + // payload. |
|
167 | + $attributes = array_merge($attributes, array( |
|
168 | + 'jpegphoto', |
|
169 | + 'thumbnailphoto' |
|
170 | + )); |
|
171 | + } |
|
172 | + |
|
173 | + return $attributes; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Checks whether the specified user is marked as deleted |
|
178 | + * @param string $id the ownCloud user name |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function isDeletedUser($id) { |
|
182 | + $isDeleted = $this->ocConfig->getUserValue( |
|
183 | + $id, 'user_ldap', 'isDeleted', 0); |
|
184 | + return intval($isDeleted) === 1; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * creates and returns an instance of OfflineUser for the specified user |
|
189 | + * @param string $id |
|
190 | + * @return \OCA\User_LDAP\User\OfflineUser |
|
191 | + */ |
|
192 | + public function getDeletedUser($id) { |
|
193 | + return new OfflineUser( |
|
194 | + $id, |
|
195 | + $this->ocConfig, |
|
196 | + $this->db, |
|
197 | + $this->access->getUserMapper()); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * @brief returns a User object by it's ownCloud username |
|
202 | + * @param string $id the DN or username of the user |
|
203 | + * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null |
|
204 | + */ |
|
205 | + protected function createInstancyByUserName($id) { |
|
206 | + //most likely a uid. Check whether it is a deleted user |
|
207 | + if($this->isDeletedUser($id)) { |
|
208 | + return $this->getDeletedUser($id); |
|
209 | + } |
|
210 | + $dn = $this->access->username2dn($id); |
|
211 | + if($dn !== false) { |
|
212 | + return $this->createAndCache($dn, $id); |
|
213 | + } |
|
214 | + return null; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @brief returns a User object by it's DN or ownCloud username |
|
219 | + * @param string $id the DN or username of the user |
|
220 | + * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null |
|
221 | + * @throws \Exception when connection could not be established |
|
222 | + */ |
|
223 | + public function get($id) { |
|
224 | + $this->checkAccess(); |
|
225 | + if(isset($this->usersByDN[$id])) { |
|
226 | + return $this->usersByDN[$id]; |
|
227 | + } else if(isset($this->usersByUid[$id])) { |
|
228 | + return $this->usersByUid[$id]; |
|
229 | + } |
|
230 | + |
|
231 | + if($this->access->stringResemblesDN($id) ) { |
|
232 | + $uid = $this->access->dn2username($id); |
|
233 | + if($uid !== false) { |
|
234 | + return $this->createAndCache($id, $uid); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + return $this->createInstancyByUserName($id); |
|
239 | + } |
|
240 | 240 | |
241 | 241 | } |