source: branches/2.3/phpgwapi/inc/class.auth_ldap.inc.php @ 5118

Revision 5118, 10.8 KB checked in by brunocosta, 12 years ago (diff)

Ticket #2299 - Trata o erro 53 do LDAP na autenticação do usuário.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**************************************************************************\
3        * eGroupWare API - Auth from LDAP                                          *
4        * This file written by Lars Kneschke <lkneschke@linux-at-work.de>          *
5        * and Joseph Engo <jengo@phpgroupware.org>                                 *
6        * Authentication based on LDAP Server                                      *
7        * Copyright (C) 2000, 2001 Joseph Engo                                     *
8        * Copyright (C) 2002, 2003 Lars Kneschke                                   *
9        * -------------------------------------------------------------------------*
10        * This library is part of the eGroupWare API                               *
11        * http://www.egroupware.org/api                                            *
12        * ------------------------------------------------------------------------ *
13        * This library is free software; you can redistribute it and/or modify it  *
14        * under the terms of the GNU Lesser General Public License as published by *
15        * the Free Software Foundation; either version 2.1 of the License,         *
16        * or any later version.                                                    *
17        * This library is distributed in the hope that it will be useful, but      *
18        * WITHOUT ANY WARRANTY; without even the implied warranty of               *
19        * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
20        * See the GNU Lesser General Public License for more details.              *
21        * You should have received a copy of the GNU Lesser General Public License *
22        * along with this library; if not, write to the Free Software Foundation,  *
23        * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
24        \**************************************************************************/
25
26 
27        class auth_
28        {
29                var $previous_login = -1;
30
31                function authenticate($username, $passwd)
32                {
33                        if (ereg('[()|&=*,<>!~]',$username))
34                        {
35                                return False;
36                        }
37
38                        if(!$ldap = @ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']))
39                        {
40                                $GLOBALS['phpgw']->log->message('F-Abort, Failed connecting to LDAP server for authenication, execution stopped');
41                                $GLOBALS['phpgw']->log->commit();
42                                return False;
43                        }
44
45                        if($GLOBALS['phpgw_info']['server']['ldap_version3'])
46                        {
47                                ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
48                        }
49
50                        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
51
52                        /* Login with the LDAP Admin. User to find the User DN.  */
53                        if(!@ldap_bind($ldap, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']))
54                        {
55                                return False;
56                        }
57                        /* find the dn for this uid, the uid is not always in the dn */
58                        $attributes     = array('uid','dn','givenName','sn','mail','uidNumber','gidNumber','usercertificate');
59                        $filter = $GLOBALS['phpgw_info']['server']['ldap_search_filter'] ? $GLOBALS['phpgw_info']['server']['ldap_search_filter'] : '(uid=%user)';
60                        $filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['phpgw_info']['user']['domain']),$filter);
61
62                        if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap')
63                        {
64                                $filter = "(&$filter(phpgwaccountstatus=A))";
65                        }
66
67                        $sri = ldap_search($ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $attributes);
68                        $allValues = ldap_get_entries($ldap, $sri);
69
70                        if ($allValues['count'] > 0)
71                        {
72                                if($GLOBALS['phpgw_info']['server']['case_sensitive_username'] == true)
73                                {
74                                        if($allValues[0]['uid'][0] != $username)
75                                        {
76                                                return false;
77                                        }
78                                }
79                                if($_SESSION['login_certificate'])
80                                {
81                                        # certificate not equal, not authenticate.
82                                        if($allValues[0]['usercertificate'][0] != $_SESSION['login_certificate'])
83                                        {
84                                                return false;
85                                        }
86                                }
87                                /* we only care about the first dn */
88                                $userDN = $allValues[0]['dn'];
89                                /*
90                                generate a bogus password to pass if the user doesn't give us one
91                                this gets around systems that are anonymous search enabled
92                                */
93                                if (empty($passwd))
94                                {
95                                        $passwd = crypt(microtime());
96                                }
97                                /* try to bind as the user with user suplied password */
98                                if (@ldap_bind($ldap, $userDN, $passwd))
99                                {
100                                        if ($GLOBALS['phpgw_info']['server']['account_repository'] != 'ldap')
101                                        {
102                                                $account = CreateObject('phpgwapi.accounts',$username,'u');
103                                                if (!$account->account_id && $GLOBALS['phpgw_info']['server']['auto_create_acct'])
104                                                {
105                                                        // create a global array with all availible info about that account
106                                                        $GLOBALS['auto_create_acct'] = array();
107                                                        foreach(array(
108                                                                'givenname' => 'firstname',
109                                                                'sn'        => 'lastname',
110                                                                'uidnumber' => 'id',
111                                                                'mail'      => 'email',
112                                                                'gidnumber' => 'primary_group',
113                                                        ) as $ldap_name => $acct_name)
114                                                        {
115                                                                $GLOBALS['auto_create_acct'][$acct_name] =
116                                                                        $GLOBALS['phpgw']->translation->convert($allValues[0][$ldap_name][0],'utf-8');
117                                                        }
118                                                        return True;
119                                                }
120                                                $data = $account->read_repository();
121                                                return $data['status'] == 'A';
122                                        }
123                                        return True;
124                                }else{
125                                    if(ldap_errno($ldap) == 53)
126                                        return 0;
127                                }
128                        }
129                        /* dn not found or password wrong */
130                        return False;
131                }
132
133                function change_password($old_passwd, $new_passwd, $_account_id='')
134                {
135                        if ('' == $_account_id)
136                        {
137                                $username = $GLOBALS['phpgw_info']['user']['account_lid'];
138                        }
139                        else
140                        {
141                                $username = $GLOBALS['phpgw']->accounts->id2name($_account_id);
142                        }
143                        $filter = $GLOBALS['phpgw_info']['server']['ldap_search_filter'] ? $GLOBALS['phpgw_info']['server']['ldap_search_filter'] : '(uid=%user)';
144                        $filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['phpgw_info']['user']['domain']),$filter);
145
146                        // LDAP Replication mode.
147                        if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
148                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
149                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
150                        {
151                                $ds = $GLOBALS['phpgw']->common->ldapConnect(
152                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_host'],
153                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
154                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']
155                                                                                           );
156                        }
157                        else
158                        {
159                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
160                        }
161
162                        $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
163                        $allValues = ldap_get_entries($ds, $sri);
164                        $entry['userpassword'] = $this->encrypt_password($new_passwd);
165                        $entry['phpgwlastpasswdchange'] = time();
166                       
167                        /* SAMBA Begin's*/
168                        foreach ($allValues[0]['objectclass'] as $objectclass)
169                        {
170                                if ($objectclass == 'sambaSamAccount')
171                                {
172                                        $entry['sambaLMPassword'] = exec( "/home/expressolivre/mkntpwd -L '{$new_passwd}'" );
173                                        $entry['sambaNTPassword'] = exec( "/home/expressolivre/mkntpwd -N '{$new_passwd}'" );
174                                }
175                        }
176                        /* SAMBA End's*/
177                       
178                        $dn = $allValues[0]['dn'];
179                       
180                        /* userPasswordRFC2617 Begin's*/
181                        $c = CreateObject('phpgwapi.config','expressoAdmin1_2');
182                        $c->read_repository();
183                        $current_config = $c->config_data;
184                        if ($current_config['expressoAdmin_userPasswordRFC2617'] == 'true')
185                        {
186                                $realm          = $current_config['expressoAdmin_realm_userPasswordRFC2617'];
187                                $uid            = $allValues[0]['uid'][0];
188                                $password       = $new_passwd;
189                                $passUserRFC2617 = $realm . ':      ' . md5("$uid:$realm:$password");
190
191                                if ($allValues[0]['userpasswordrfc2617'][0] != '')
192                                        $entry['userPasswordRFC2617'] = $passUserRFC2617;
193                                else
194                                {
195                                        $ldap_add['userPasswordRFC2617'] = $passUserRFC2617;
196                                        if (!@ldap_mod_add($ds, $dn, $ldap_add))
197                                        {
198                                                return false;
199                                        }
200                                }
201                        }
202                        /* userPasswordRFC2617 End's*/
203                       
204                        if (!@ldap_modify($ds, $dn, $entry))
205                        {
206                                return false;
207                        }
208                        $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
209                        return $new_passwd;
210                }
211
212                function change_password_user ($old_passwd, $new_passwd, $dn, $referrals=false)
213                {
214                        if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
215                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
216                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
217                        {
218                                $ds = $GLOBALS['phpgw']->common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_master_host'],
219                                                $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
220                                                $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
221                        }
222                        else
223                        {
224                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
225                        }
226                        if (!$ds)
227                                {
228                                $this->auth_reason = ldap_errno($ldap);
229                                return False;
230                                }
231                        else
232                        {
233                                if ($referrals)
234                                        {
235                                        $this->passwd=$old_passwd;
236                                        $this->dn=$dn;
237                                        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
238                                        ldap_set_option($ds, LDAP_OPT_REFERRALS, 1);
239                                        if($GLOBALS['phpgw_info']['server']['diretorioescravo'])
240                                                {
241                                                ldap_set_rebind_proc($ds, array($this, '_rebindProc'));
242                                                }
243                                        }
244                                $modify["userpassword"]=$new_passwd;
245                                if (!@ldap_bind($ds,$dn,$old_passwd))
246                                        {
247                                        if (!@ldap_mod_replace($ds,$dn,$modify))
248                                                {
249                                                $this->auth_reason = ldap_errno($ds);
250                                                return false;
251                                                }
252                                                else
253                                                {
254                                                    $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
255                                                return $new_passwd;
256                                                }
257                                        $this->auth_reason = ldap_errno($ds);
258                                        return False;
259                                        }
260                                        else
261                                        {
262                                        if (!ldap_mod_replace($ds,$dn,$modify))
263                                                {
264                                                $this->auth_reason = ldap_errno($ds);
265                                                return False;
266                                                }
267                                                else
268                                                {
269                                                $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
270                                                return $new_passwd;
271                                                }
272                                        }
273                        }
274                }
275
276
277                function update_lastlogin($_account_id, $ip)
278                {
279                        if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap')
280                        {
281                                $entry['phpgwaccountlastlogin']     = time();
282                                $entry['phpgwaccountlastloginfrom'] = $ip;
283       
284                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
285                                $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], 'uidnumber=' . (int)$_account_id);
286                                $allValues = ldap_get_entries($ds, $sri);
287       
288                                $dn = $allValues[0]['dn'];
289                                $this->previous_login = $allValues[0]['phpgwaccountlastlogin'][0];
290       
291                                @ldap_modify($ds, $dn, $entry);
292                        }
293                        else
294                        {
295                                $GLOBALS['phpgw']->db->query("select account_lastlogin from phpgw_accounts where account_id='$_account_id'",__LINE__,__FILE__);
296                                $GLOBALS['phpgw']->db->next_record();
297                                $this->previous_login = $GLOBALS['phpgw']->db->f('account_lastlogin');
298       
299                                $GLOBALS['phpgw']->db->query("update phpgw_accounts set account_lastloginfrom='"
300                                        . "$ip', account_lastlogin='" . time()
301                                        . "' where account_id='$_account_id'",__LINE__,__FILE__);
302                        }
303                }
304        }
305?>
Note: See TracBrowser for help on using the repository browser.