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

Revision 4867, 10.6 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #2169 - Disponibilizar certificado na sessã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                                }
125                        }
126                        /* dn not found or password wrong */
127                        return False;
128                }
129
130                function change_password($old_passwd, $new_passwd, $_account_id='')
131                {
132                        if ('' == $_account_id)
133                        {
134                                $username = $GLOBALS['phpgw_info']['user']['account_lid'];
135                        }
136                        else
137                        {
138                                $username = $GLOBALS['phpgw']->accounts->id2name($_account_id);
139                        }
140                        $filter = $GLOBALS['phpgw_info']['server']['ldap_search_filter'] ? $GLOBALS['phpgw_info']['server']['ldap_search_filter'] : '(uid=%user)';
141                        $filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['phpgw_info']['user']['domain']),$filter);
142
143                        // LDAP Replication mode.
144                        if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
145                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
146                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
147                        {
148                                $ds = $GLOBALS['phpgw']->common->ldapConnect(
149                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_host'],
150                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
151                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']
152                                                                                           );
153                        }
154                        else
155                        {
156                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
157                        }
158
159                        $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
160                        $allValues = ldap_get_entries($ds, $sri);
161                        $entry['userpassword'] = $this->encrypt_password($new_passwd);
162                        $entry['phpgwlastpasswdchange'] = time();
163                       
164                        /* SAMBA Begin's*/
165                        foreach ($allValues[0]['objectclass'] as $objectclass)
166                        {
167                                if ($objectclass == 'sambaSamAccount')
168                                {
169                                        $entry['sambaLMPassword'] = exec( "/home/expressolivre/mkntpwd -L '{$new_passwd}'" );
170                                        $entry['sambaNTPassword'] = exec( "/home/expressolivre/mkntpwd -N '{$new_passwd}'" );
171                                }
172                        }
173                        /* SAMBA End's*/
174                       
175                        $dn = $allValues[0]['dn'];
176                       
177                        /* userPasswordRFC2617 Begin's*/
178                        $c = CreateObject('phpgwapi.config','expressoAdmin1_2');
179                        $c->read_repository();
180                        $current_config = $c->config_data;
181                        if ($current_config['expressoAdmin_userPasswordRFC2617'] == 'true')
182                        {
183                                $realm          = $current_config['expressoAdmin_realm_userPasswordRFC2617'];
184                                $uid            = $allValues[0]['uid'][0];
185                                $password       = $new_passwd;
186                                $passUserRFC2617 = $realm . ':      ' . md5("$uid:$realm:$password");
187
188                                if ($allValues[0]['userpasswordrfc2617'][0] != '')
189                                        $entry['userPasswordRFC2617'] = $passUserRFC2617;
190                                else
191                                {
192                                        $ldap_add['userPasswordRFC2617'] = $passUserRFC2617;
193                                        if (!@ldap_mod_add($ds, $dn, $ldap_add))
194                                        {
195                                                return false;
196                                        }
197                                }
198                        }
199                        /* userPasswordRFC2617 End's*/
200                       
201                        if (!@ldap_modify($ds, $dn, $entry))
202                        {
203                                return false;
204                        }
205                        $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
206                        return $new_passwd;
207                }
208
209                function change_password_user ($old_passwd, $new_passwd, $dn, $referrals=false)
210                {
211                        if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
212                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
213                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
214                        {
215                                $ds = $GLOBALS['phpgw']->common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_master_host'],
216                                                $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
217                                                $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
218                        }
219                        else
220                        {
221                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
222                        }
223                        if (!$ds)
224                                {
225                                $this->auth_reason = ldap_errno($ldap);
226                                return False;
227                                }
228                        else
229                        {
230                                if ($referrals)
231                                        {
232                                        $this->passwd=$old_passwd;
233                                        $this->dn=$dn;
234                                        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
235                                        ldap_set_option($ds, LDAP_OPT_REFERRALS, 1);
236                                        if($GLOBALS['phpgw_info']['server']['diretorioescravo'])
237                                                {
238                                                ldap_set_rebind_proc($ds, array($this, '_rebindProc'));
239                                                }
240                                        }
241                                $modify["userpassword"]=$new_passwd;
242                                if (!@ldap_bind($ds,$dn,$old_passwd))
243                                        {
244                                        if (!@ldap_mod_replace($ds,$dn,$modify))
245                                                {
246                                                $this->auth_reason = ldap_errno($ds);
247                                                return false;
248                                                }
249                                                else
250                                                {
251                                                    $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
252                                                return $new_passwd;
253                                                }
254                                        $this->auth_reason = ldap_errno($ds);
255                                        return False;
256                                        }
257                                        else
258                                        {
259                                        if (!ldap_mod_replace($ds,$dn,$modify))
260                                                {
261                                                $this->auth_reason = ldap_errno($ds);
262                                                return False;
263                                                }
264                                                else
265                                                {
266                                                $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
267                                                return $new_passwd;
268                                                }
269                                        }
270                        }
271                }
272
273
274                function update_lastlogin($_account_id, $ip)
275                {
276                        if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap')
277                        {
278                                $entry['phpgwaccountlastlogin']     = time();
279                                $entry['phpgwaccountlastloginfrom'] = $ip;
280       
281                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
282                                $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], 'uidnumber=' . (int)$_account_id);
283                                $allValues = ldap_get_entries($ds, $sri);
284       
285                                $dn = $allValues[0]['dn'];
286                                $this->previous_login = $allValues[0]['phpgwaccountlastlogin'][0];
287       
288                                @ldap_modify($ds, $dn, $entry);
289                        }
290                        else
291                        {
292                                $GLOBALS['phpgw']->db->query("select account_lastlogin from phpgw_accounts where account_id='$_account_id'",__LINE__,__FILE__);
293                                $GLOBALS['phpgw']->db->next_record();
294                                $this->previous_login = $GLOBALS['phpgw']->db->f('account_lastlogin');
295       
296                                $GLOBALS['phpgw']->db->query("update phpgw_accounts set account_lastloginfrom='"
297                                        . "$ip', account_lastlogin='" . time()
298                                        . "' where account_id='$_account_id'",__LINE__,__FILE__);
299                        }
300                }
301        }
302?>
Note: See TracBrowser for help on using the repository browser.