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