]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LdapAuthentication/LdapAuthenticationPlugin.php
set provider global JS variable from Mapstraction
[quix0rs-gnu-social.git] / plugins / LdapAuthentication / LdapAuthenticationPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to enable LDAP Authentication and 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/Authentication/AuthenticationPlugin.php';
35 require_once 'Net/LDAP2.php';
36
37 class LdapAuthenticationPlugin extends AuthenticationPlugin
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 $attributes=array();
50
51     function onInitializePlugin(){
52         parent::onInitializePlugin();
53         if(!isset($this->host)){
54             throw new Exception("must specify a host");
55         }
56         if(!isset($this->basedn)){
57             throw new Exception("must specify a basedn");
58         }
59         if(!isset($this->attributes['nickname'])){
60             throw new Exception("must specify a nickname attribute");
61         }
62         if(!isset($this->attributes['username'])){
63             throw new Exception("must specify a username attribute");
64         }
65     }
66     
67     //---interface implementation---//
68
69     function checkPassword($username, $password)
70     {
71         $ldap = $this->ldap_get_connection();
72         if(!$ldap){
73             return false;
74         }
75         $entry = $this->ldap_get_user($username);
76         if(!$entry){
77             return false;
78         }else{
79             $config = $this->ldap_get_config();
80             $config['binddn']=$entry->dn();
81             $config['bindpw']=$password;
82             if($this->ldap_get_connection($config)){
83                 return true;
84             }else{
85                 return false;
86             }
87         }
88     }
89
90     function autoRegister($username)
91     {
92         $entry = $this->ldap_get_user($username,$this->attributes);
93         if($entry){
94             $registration_data = array();
95             foreach($this->attributes as $sn_attribute=>$ldap_attribute){
96                 $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
97             }
98             if(isset($registration_data['email']) && !empty($registration_data['email'])){
99                 $registration_data['email_confirmed']=true;
100             }
101             //set the database saved password to a random string.
102             $registration_data['password']=common_good_rand(16);
103             return User::register($registration_data);
104         }else{
105             //user isn't in ldap, so we cannot register him
106             return false;
107         }
108     }
109
110     function changePassword($username,$oldpassword,$newpassword)
111     {
112         //TODO implement this
113         throw new Exception(_('Sorry, changing LDAP passwords is not supported at this time'));
114
115         return false;
116     }
117     
118     //---utility functions---//
119     function ldap_get_config(){
120         $config = array();
121         $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','filter','scope');
122         foreach($keys as $key){
123             $value = $this->$key;
124             if($value!==null){
125                 $config[$key]=$value;
126             }
127         }
128         return $config;
129     }
130     
131     function ldap_get_connection($config = null){
132         if($config == null){
133             $config = $this->ldap_get_config();
134         }
135         
136         //cannot use Net_LDAP2::connect() as StatusNet uses
137         //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
138         //PEAR handling can be overridden on instance objects, so we do that.
139         $ldap = new Net_LDAP2($config);
140         $ldap->setErrorHandling(PEAR_ERROR_RETURN);
141         $err=$ldap->bind();
142         if (Net_LDAP2::isError($err)) {
143             common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
144             return false;
145         }
146         return $ldap;
147     }
148     
149     /**
150      * get an LDAP entry for a user with a given username
151      * 
152      * @param string $username
153      * $param array $attributes LDAP attributes to retrieve
154      * @return string DN
155      */
156     function ldap_get_user($username,$attributes=array()){
157         $ldap = $this->ldap_get_connection();
158         $filter = Net_LDAP2_Filter::create($this->attributes['username'], 'equals',  $username);
159         $options = array(
160             'scope' => 'sub',
161             'attributes' => $attributes
162         );
163         $search = $ldap->search(null,$filter,$options);
164         
165         if (PEAR::isError($search)) {
166             common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
167             return false;
168         }
169
170         if($search->count()==0){
171             return false;
172         }else if($search->count()==1){
173             $entry = $search->shiftEntry();
174             return $entry;
175         }else{
176             common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);
177             return false;
178         }
179     }
180 }