group_ldap.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * ownCloud – LDAP group backend
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2012 Arthur Schiwon blizzz@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\user_ldap;
  23. class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
  24. protected $enabled = false;
  25. public function setConnector(lib\Connection &$connection) {
  26. parent::setConnector($connection);
  27. $filter = $this->connection->ldapGroupFilter;
  28. $gassoc = $this->connection->ldapGroupMemberAssocAttr;
  29. if(!empty($filter) && !empty($gassoc)) {
  30. $this->enabled = true;
  31. }
  32. }
  33. /**
  34. * @brief is user in group?
  35. * @param $uid uid of the user
  36. * @param $gid gid of the group
  37. * @returns true/false
  38. *
  39. * Checks whether the user is member of a group or not.
  40. */
  41. public function inGroup($uid, $gid) {
  42. if(!$this->enabled) {
  43. return false;
  44. }
  45. if($this->connection->isCached('inGroup'.$uid.':'.$gid)) {
  46. return $this->connection->getFromCache('inGroup'.$uid.':'.$gid);
  47. }
  48. $dn_user = $this->username2dn($uid);
  49. $dn_group = $this->groupname2dn($gid);
  50. // just in case
  51. if(!$dn_group || !$dn_user) {
  52. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  53. return false;
  54. }
  55. //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course.
  56. $members = $this->readAttribute($dn_group, $this->connection->ldapGroupMemberAssocAttr);
  57. if(!$members) {
  58. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
  59. return false;
  60. }
  61. //extra work if we don't get back user DNs
  62. //TODO: this can be done with one LDAP query
  63. if(strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid') {
  64. $dns = array();
  65. foreach($members as $mid) {
  66. $filter = str_replace('%uid', $mid, $this->connection->ldapLoginFilter);
  67. $ldap_users = $this->fetchListOfUsers($filter, 'dn');
  68. if(count($ldap_users) < 1) {
  69. continue;
  70. }
  71. $dns[] = $ldap_users[0];
  72. }
  73. $members = $dns;
  74. }
  75. $isInGroup = in_array($dn_user, $members);
  76. $this->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
  77. return $isInGroup;
  78. }
  79. /**
  80. * @brief Get all groups a user belongs to
  81. * @param $uid Name of the user
  82. * @returns array with group names
  83. *
  84. * This function fetches all groups a user belongs to. It does not check
  85. * if the user exists at all.
  86. */
  87. public function getUserGroups($uid) {
  88. if(!$this->enabled) {
  89. return array();
  90. }
  91. $cacheKey = 'getUserGroups'.$uid;
  92. if($this->connection->isCached($cacheKey)) {
  93. return $this->connection->getFromCache($cacheKey);
  94. }
  95. $userDN = $this->username2dn($uid);
  96. if(!$userDN) {
  97. $this->connection->writeToCache($cacheKey, array());
  98. return array();
  99. }
  100. //uniqueMember takes DN, memberuid the uid, so we need to distinguish
  101. if((strtolower($this->connection->ldapGroupMemberAssocAttr) == 'uniquemember')
  102. || (strtolower($this->connection->ldapGroupMemberAssocAttr) == 'member')
  103. ) {
  104. $uid = $userDN;
  105. } else if(strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid') {
  106. $result = $this->readAttribute($userDN, 'uid');
  107. $uid = $result[0];
  108. } else {
  109. // just in case
  110. $uid = $userDN;
  111. }
  112. $filter = $this->combineFilterWithAnd(array(
  113. $this->connection->ldapGroupFilter,
  114. $this->connection->ldapGroupMemberAssocAttr.'='.$uid
  115. ));
  116. $groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'));
  117. $groups = array_unique($this->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
  118. $this->connection->writeToCache($cacheKey, $groups);
  119. return $groups;
  120. }
  121. /**
  122. * @brief get a list of all users in a group
  123. * @returns array with user ids
  124. */
  125. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  126. if(!$this->enabled) {
  127. return array();
  128. }
  129. $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
  130. // check for cache of the exact query
  131. $groupUsers = $this->connection->getFromCache($cachekey);
  132. if(!is_null($groupUsers)) {
  133. return $groupUsers;
  134. }
  135. // check for cache of the query without limit and offset
  136. $groupUsers = $this->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
  137. if(!is_null($groupUsers)) {
  138. $groupUsers = array_slice($groupUsers, $offset, $limit);
  139. $this->connection->writeToCache($cachekey, $groupUsers);
  140. return $groupUsers;
  141. }
  142. if($limit == -1) {
  143. $limit = null;
  144. }
  145. $groupDN = $this->groupname2dn($gid);
  146. if(!$groupDN) {
  147. // group couldn't be found, return empty resultset
  148. $this->connection->writeToCache($cachekey, array());
  149. return array();
  150. }
  151. $members = $this->readAttribute($groupDN, $this->connection->ldapGroupMemberAssocAttr);
  152. if(!$members) {
  153. //in case users could not be retrieved, return empty resultset
  154. $this->connection->writeToCache($cachekey, array());
  155. return array();
  156. }
  157. $search = empty($search) ? '*' : '*'.$search.'*';
  158. $groupUsers = array();
  159. $isMemberUid = (strtolower($this->connection->ldapGroupMemberAssocAttr) == 'memberuid');
  160. foreach($members as $member) {
  161. if($isMemberUid) {
  162. //we got uids, need to get their DNs to 'tranlsate' them to usernames
  163. $filter = $this->combineFilterWithAnd(array(
  164. \OCP\Util::mb_str_replace('%uid', $member, $this->connection>ldapLoginFilter, 'UTF-8'),
  165. $this->connection->ldapUserDisplayName.'='.$search
  166. ));
  167. $ldap_users = $this->fetchListOfUsers($filter, 'dn');
  168. if(count($ldap_users) < 1) {
  169. continue;
  170. }
  171. $groupUsers[] = $this->dn2username($ldap_users[0]);
  172. } else {
  173. //we got DNs, check if we need to filter by search or we can give back all of them
  174. if($search != '*') {
  175. if(!$this->readAttribute($member, $this->connection->ldapUserDisplayName, $this->connection->ldapUserDisplayName.'='.$search)) {
  176. continue;
  177. }
  178. }
  179. // dn2username will also check if the users belong to the allowed base
  180. if($ocname = $this->dn2username($member)) {
  181. $groupUsers[] = $ocname;
  182. }
  183. }
  184. }
  185. natsort($groupUsers);
  186. $this->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
  187. $groupUsers = array_slice($groupUsers, $offset, $limit);
  188. $this->connection->writeToCache($cachekey, $groupUsers);
  189. return $groupUsers;
  190. }
  191. /**
  192. * @brief get a list of all groups
  193. * @returns array with group names
  194. *
  195. * Returns a list with all groups
  196. */
  197. public function getGroups($search = '', $limit = -1, $offset = 0) {
  198. if(!$this->enabled) {
  199. return array();
  200. }
  201. $cachekey = 'getGroups-'.$search.'-'.$limit.'-'.$offset;
  202. //Check cache before driving unnecessary searches
  203. \OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG);
  204. $ldap_groups = $this->connection->getFromCache($cachekey);
  205. if(!is_null($ldap_groups)) {
  206. return $ldap_groups;
  207. }
  208. // if we'd pass -1 to LDAP search, we'd end up in a Protocol error. With a limit of 0, we get 0 results. So we pass null.
  209. if($limit <= 0) {
  210. $limit = null;
  211. }
  212. $search = empty($search) ? '*' : '*'.$search.'*';
  213. $filter = $this->combineFilterWithAnd(array(
  214. $this->connection->ldapGroupFilter,
  215. $this->connection->ldapGroupDisplayName.'='.$search
  216. ));
  217. \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
  218. $ldap_groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'), $limit, $offset);
  219. $ldap_groups = $this->ownCloudGroupNames($ldap_groups);
  220. $this->connection->writeToCache($cachekey, $ldap_groups);
  221. return $ldap_groups;
  222. }
  223. public function groupMatchesFilter($group) {
  224. return (strripos($group, $this->groupSearch) !== false);
  225. }
  226. /**
  227. * check if a group exists
  228. * @param string $gid
  229. * @return bool
  230. */
  231. public function groupExists($gid) {
  232. if($this->connection->isCached('groupExists'.$gid)) {
  233. return $this->connection->getFromCache('groupExists'.$gid);
  234. }
  235. //getting dn, if false the group does not exist. If dn, it may be mapped only, requires more checking.
  236. $dn = $this->groupname2dn($gid);
  237. if(!$dn) {
  238. $this->connection->writeToCache('groupExists'.$gid, false);
  239. return false;
  240. }
  241. //if group really still exists, we will be able to read its objectclass
  242. $objcs = $this->readAttribute($dn, 'objectclass');
  243. if(!$objcs || empty($objcs)) {
  244. $this->connection->writeToCache('groupExists'.$gid, false);
  245. return false;
  246. }
  247. $this->connection->writeToCache('groupExists'.$gid, true);
  248. return true;
  249. }
  250. /**
  251. * @brief Check if backend implements actions
  252. * @param $actions bitwise-or'ed actions
  253. * @returns boolean
  254. *
  255. * Returns the supported actions as int to be
  256. * compared with OC_USER_BACKEND_CREATE_USER etc.
  257. */
  258. public function implementsActions($actions) {
  259. //always returns false, because possible actions are modifying actions. We do not write to LDAP, at least for now.
  260. return false;
  261. }
  262. }