]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LdapAuthorization/LdapAuthorizationPlugin.php
some formatting changes to make inblobs work
[quix0rs-gnu-social.git] / plugins / LdapAuthorization / LdapAuthorizationPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to enable LDAP Authorization
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @copyright 2009 Craig Andrews http://candrews.integralblue.com
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/Authorization/AuthorizationPlugin.php';
35 require_once 'Net/LDAP2.php';
36
37 class LdapAuthorizationPlugin extends AuthorizationPlugin
38 {
39     public $host=null;
40     public $port=null;
41     public $version=null;
42     public $starttls=null;
43     public $binddn=null;
44     public $bindpw=null;
45     public $basedn=null;
46     public $options=null;
47     public $filter=null;
48     public $scope=null;
49     public $provider_name = null;
50     public $uniqueMember_attribute = null;
51     public $roles_to_groups = array();
52     public $login_group = null;
53     public $attributes = array();
54
55     function onInitializePlugin(){
56         parent::onInitializePlugin();
57         if(!isset($this->host)){
58             throw new Exception("must specify a host");
59         }
60         if(!isset($this->basedn)){
61             throw new Exception("must specify a basedn");
62         }
63         if(!isset($this->provider_name)){
64             throw new Exception("provider_name must be set. Use the provider_name from the LDAP Authentication plugin.");
65         }
66         if(!isset($this->uniqueMember_attribute)){
67             throw new Exception("uniqueMember_attribute must be set.");
68         }
69         if(!isset($this->attributes['username'])){
70             throw new Exception("username attribute must be set.");
71         }
72     }
73
74     //---interface implementation---//
75     function loginAllowed($user) {
76         $user_username = new User_username();
77         $user_username->user_id=$user->id;
78         $user_username->provider_name=$this->provider_name;
79         if($user_username->find() && $user_username->fetch()){
80             $entry = $this->ldap_get_user($user_username->username);
81             if($entry){
82                 if(isset($this->login_group)){
83                     if(is_array($this->login_group)){
84                         foreach($this->login_group as $group){
85                             if($this->ldap_is_dn_member_of_group($entry->dn(),$group)){
86                                 return true;
87                             }
88                         }
89                     }else{
90                         if($this->ldap_is_dn_member_of_group($entry->dn(),$this->login_group)){
91                             return true;
92                         }
93                     }
94                     return null;
95                 }else{
96                     //if a user exists, we can assume he's allowed to login
97                     return true;
98                 }
99             }else{
100                 return null;
101             }
102         }else{
103             return null;
104         }
105     }
106
107     function hasRole($profile, $name) {
108         $user_username = new User_username();
109         $user_username->user_id=$profile->id;
110         $user_username->provider_name=$this->provider_name;
111         if($user_username->find() && $user_username->fetch()){
112             $entry = $this->ldap_get_user($user_username->username);
113             if($entry){
114                 if(isset($this->roles_to_groups[$name])){
115                     if(is_array($this->roles_to_groups[$name])){
116                         foreach($this->roles_to_groups[$name] as $group){
117                             if($this->ldap_is_dn_member_of_group($entry->dn(),$group)){
118                                 return true;
119                             }
120                         }
121                     }else{
122                         if($this->ldap_is_dn_member_of_group($entry->dn(),$this->roles_to_groups[$name])){
123                             return true;
124                         }
125                     }
126                 }
127             }
128         }
129         return false;
130     }
131
132     function ldap_is_dn_member_of_group($userDn, $groupDn)
133     {
134         $ldap = $this->ldap_get_connection();
135         $link = $ldap->getLink();
136         $r = ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
137         if ($r === true){
138             return true;
139         }else if($r === false){
140             return false;
141         }else{
142             common_log(LOG_ERR, ldap_error($r));
143             return false;
144         }
145     }
146
147     function ldap_get_config(){
148         $config = array();
149         $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','filter','scope');
150         foreach($keys as $key){
151             $value = $this->$key;
152             if($value!==null){
153                 $config[$key]=$value;
154             }
155         }
156         return $config;
157     }
158
159     //-----the below function were copied from LDAPAuthenticationPlugin. They will be moved to a utility class soon.----\\
160     function ldap_get_connection($config = null){
161         if($config == null && isset($this->default_ldap)){
162             return $this->default_ldap;
163         }
164         
165         //cannot use Net_LDAP2::connect() as StatusNet uses
166         //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
167         //PEAR handling can be overridden on instance objects, so we do that.
168         $ldap = new Net_LDAP2(isset($config)?$config:$this->ldap_get_config());
169         $ldap->setErrorHandling(PEAR_ERROR_RETURN);
170         $err=$ldap->bind();
171         if (Net_LDAP2::isError($err)) {
172             common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
173             return false;
174         }
175         if($config == null) $this->default_ldap=$ldap;
176         return $ldap;
177     }
178     
179     /**
180      * get an LDAP entry for a user with a given username
181      * 
182      * @param string $username
183      * $param array $attributes LDAP attributes to retrieve
184      * @return string DN
185      */
186     function ldap_get_user($username,$attributes=array(),$ldap=null){
187         if($ldap==null) {
188             $ldap = $this->ldap_get_connection();
189         }
190         $filter = Net_LDAP2_Filter::create($this->attributes['username'], 'equals',  $username);
191         $options = array(
192             'attributes' => $attributes
193         );
194         $search = $ldap->search(null,$filter,$options);
195         
196         if (PEAR::isError($search)) {
197             common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
198             return false;
199         }
200
201         if($search->count()==0){
202             return false;
203         }else if($search->count()==1){
204             $entry = $search->shiftEntry();
205             return $entry;
206         }else{
207             common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);
208             return false;
209         }
210     }
211 }