3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to enable LDAP Authentication and Authorization
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR.'/plugins/Authentication/AuthenticationPlugin.php';
35 require_once 'Net/LDAP2.php';
37 class LdapAuthenticatonPlugin extends AuthenticationPlugin
42 public $starttls=null;
49 public $attributes=array();
51 function __construct()
53 parent::__construct();
56 //---interface implementation---//
58 function checkPassword($nickname, $password)
60 $ldap = $this->ldap_get_connection();
64 $entry = $this->ldap_get_user($nickname);
68 $config = $this->ldap_get_config();
69 $config['binddn']=$entry->dn();
70 $config['bindpw']=$password;
71 if($this->ldap_get_connection($config)){
79 function autoRegister($nickname)
82 $config_attributes = array('nickname','email','fullname','homepage','location');
83 foreach($config_attributes as $config_attribute){
84 $value = common_config('ldap', $config_attribute.'_attribute');
86 array_push($attributes,$value);
89 $entry = $this->ldap_get_user($nickname,$attributes);
91 $registration_data = array();
92 foreach($config_attributes as $config_attribute){
93 $value = common_config('ldap', $config_attribute.'_attribute');
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'));
100 $registration_data[$config_attribute]=$entry->getValue($value,'single');
104 //set the database saved password to a random string.
105 $registration_data['password']=common_good_rand(16);
106 $user = User::register($registration_data);
109 //user isn't in ldap, so we cannot register him
114 function changePassword($nickname,$oldpassword,$newpassword)
116 //TODO implement this
117 throw new Exception(_('Sorry, changing LDAP passwords is not supported at this time'));
122 function canUserChangeField($nickname, $field)
133 //---utility functions---//
134 function ldap_get_config(){
136 $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','filter','scope');
137 foreach($keys as $key){
138 $value = $this->$key;
140 $config[$key]=$value;
146 function ldap_get_connection($config = null){
148 $config = $this->ldap_get_config();
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);
157 if (Net_LDAP2::isError($err)) {
158 common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
165 * get an LDAP entry for a user with a given username
167 * @param string $username
168 * $param array $attributes LDAP attributes to retrieve
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);
176 'attributes' => $attributes
178 $search = $ldap->search(null,$filter,$options);
180 if (PEAR::isError($search)) {
181 common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
185 if($search->count()==0){
187 }else if($search->count()==1){
188 $entry = $search->shiftEntry();
191 common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);