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 LdapAuthenticationPlugin extends AuthenticationPlugin
42 public $starttls=null;
49 public $attributes=array();
51 function onInitializePlugin(){
52 parent::onInitializePlugin();
53 if(!isset($this->host)){
54 throw new Exception("must specify a host");
56 if(!isset($this->basedn)){
57 throw new Exception("must specify a basedn");
59 if(!isset($this->attributes['nickname'])){
60 throw new Exception("must specify a nickname attribute");
62 if(!isset($this->attributes['username'])){
63 throw new Exception("must specify a username attribute");
67 //---interface implementation---//
69 function checkPassword($username, $password)
71 $ldap = $this->ldap_get_connection();
75 $entry = $this->ldap_get_user($username);
79 $config = $this->ldap_get_config();
80 $config['binddn']=$entry->dn();
81 $config['bindpw']=$password;
82 if($this->ldap_get_connection($config)){
90 function autoRegister($username)
92 $entry = $this->ldap_get_user($username,$this->attributes);
94 $registration_data = array();
95 foreach($this->attributes as $sn_attribute=>$ldap_attribute){
96 $registration_data[$sn_attribute]=$entry->getValue($ldap_attribute,'single');
98 if(isset($registration_data['email']) && !empty($registration_data['email'])){
99 $registration_data['email_confirmed']=true;
101 //set the database saved password to a random string.
102 $registration_data['password']=common_good_rand(16);
103 return User::register($registration_data);
105 //user isn't in ldap, so we cannot register him
110 function changePassword($username,$oldpassword,$newpassword)
112 //TODO implement this
113 throw new Exception(_('Sorry, changing LDAP passwords is not supported at this time'));
118 //---utility functions---//
119 function ldap_get_config(){
121 $keys = array('host','port','version','starttls','binddn','bindpw','basedn','options','filter','scope');
122 foreach($keys as $key){
123 $value = $this->$key;
125 $config[$key]=$value;
131 function ldap_get_connection($config = null){
133 $config = $this->ldap_get_config();
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);
142 if (Net_LDAP2::isError($err)) {
143 common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
150 * get an LDAP entry for a user with a given username
152 * @param string $username
153 * $param array $attributes LDAP attributes to retrieve
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);
161 'attributes' => $attributes
163 $search = $ldap->search(null,$filter,$options);
165 if (PEAR::isError($search)) {
166 common_log(LOG_WARNING, 'Error while getting DN for user: '.$search->getMessage());
170 if($search->count()==0){
172 }else if($search->count()==1){
173 $entry = $search->shiftEntry();
176 common_log(LOG_WARNING, 'Found ' . $search->count() . ' ldap user with the username: ' . $username);