source: trunk/phpgwapi/inc/class.auth_ldap.inc.php @ 393

Revision 393, 8.5 KB checked in by niltonneto, 16 years ago (diff)

Opção de configuração para LDAP Master e Slaves.

  • 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');
59                       
60                        $filter = $GLOBALS['phpgw_info']['server']['ldap_search_filter'] ? $GLOBALS['phpgw_info']['server']['ldap_search_filter'] : '(uid=%user)';
61                        $filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['phpgw_info']['user']['domain']),$filter);
62
63                        if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap')
64                        {
65                                $filter = "(&$filter(phpgwaccountstatus=A))";
66                        }
67
68                        $sri = ldap_search($ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $attributes);
69                        $allValues = ldap_get_entries($ldap, $sri);
70
71                        if ($allValues['count'] > 0)
72                        {
73                                if($GLOBALS['phpgw_info']['server']['case_sensitive_username'] == true)
74                                {
75                                        if($allValues[0]['uid'][0] != $username)
76                                        {
77                                                return false;
78                                        }
79                                }
80                                /* we only care about the first dn */
81                                $userDN = $allValues[0]['dn'];
82                                /*
83                                generate a bogus password to pass if the user doesn't give us one
84                                this gets around systems that are anonymous search enabled
85                                */
86                                if (empty($passwd))
87                                {
88                                        $passwd = crypt(microtime());
89                                }
90                                /* try to bind as the user with user suplied password */
91                                if (@ldap_bind($ldap, $userDN, $passwd))
92                                {
93                                        if ($GLOBALS['phpgw_info']['server']['account_repository'] != 'ldap')
94                                        {
95                                                $account = CreateObject('phpgwapi.accounts',$username,'u');
96                                                if (!$account->account_id && $GLOBALS['phpgw_info']['server']['auto_create_acct'])
97                                                {
98                                                        // create a global array with all availible info about that account
99                                                        $GLOBALS['auto_create_acct'] = array();
100                                                        foreach(array(
101                                                                'givenname' => 'firstname',
102                                                                'sn'        => 'lastname',
103                                                                'uidnumber' => 'id',
104                                                                'mail'      => 'email',
105                                                                'gidnumber' => 'primary_group',
106                                                        ) as $ldap_name => $acct_name)
107                                                        {
108                                                                $GLOBALS['auto_create_acct'][$acct_name] =
109                                                                        $GLOBALS['phpgw']->translation->convert($allValues[0][$ldap_name][0],'utf-8');
110                                                        }
111                                                        return True;
112                                                }
113                                                $data = $account->read_repository();
114                                                return $data['status'] == 'A';
115                                        }
116                                        return True;
117                                }
118                        }
119                        /* dn not found or password wrong */
120                        return False;
121                }
122
123                function change_password($old_passwd, $new_passwd, $_account_id='')
124                {
125                        if ('' == $_account_id)
126                        {
127                                $username = $GLOBALS['phpgw_info']['user']['account_lid'];
128                        }
129                        else
130                        {
131                                $username = $GLOBALS['phpgw']->accounts->id2name($_account_id);
132                        }
133                        $filter = $GLOBALS['phpgw_info']['server']['ldap_search_filter'] ? $GLOBALS['phpgw_info']['server']['ldap_search_filter'] : '(uid=%user)';
134                        $filter = str_replace(array('%user','%domain'),array($username,$GLOBALS['phpgw_info']['user']['domain']),$filter);
135
136                        // LDAP Replication mode.
137                        if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
138                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
139                                 (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
140                        {
141                                $ds = $GLOBALS['phpgw']->common->ldapConnect(
142                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_host'],
143                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
144                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']
145                                                                                           );
146                        }
147                        else
148                        {
149                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
150                        }
151
152                        $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
153                        $allValues = ldap_get_entries($ds, $sri);
154                        $entry['userpassword'] = $this->encrypt_password($new_passwd);
155                        $entry['phpgwlastpasswdchange'] = time();
156                       
157                        /* SAMBA Begin's*/
158                        foreach ($allValues[0]['objectclass'] as $objectclass)
159                        {
160                                if ($objectclass == 'sambaSamAccount')
161                                {
162                                        $entry['sambaLMPassword'] = exec('/home/expressolivre/mkntpwd -L '.$new_passwd);
163                                        $entry['sambaNTPassword'] = exec('/home/expressolivre/mkntpwd -N '.$new_passwd);
164                                }
165                        }
166                        /* SAMBA End's*/
167                       
168                        $dn = $allValues[0]['dn'];
169                       
170                        /* userPasswordRFC2617 Begin's*/
171                        $c = CreateObject('phpgwapi.config','expressoAdmin1_2');
172                        $c->read_repository();
173                        $current_config = $c->config_data;
174                        if ($current_config['expressoAdmin_userPasswordRFC2617'] == 'true')
175                        {
176                                $realm          = $current_config['expressoAdmin_realm_userPasswordRFC2617'];
177                                $uid            = $allValues[0]['uid'][0];
178                                $password       = $new_passwd;
179                                $passUserRFC2617 = $realm . ':      ' . md5("$uid:$realm:$password");
180
181                                if ($allValues[0]['userpasswordrfc2617'][0] != '')
182                                        $entry['userPasswordRFC2617'] = $passUserRFC2617;
183                                else
184                                {
185                                        $ldap_add['userPasswordRFC2617'] = $passUserRFC2617;
186                                        if (!@ldap_mod_add($ds, $dn, $ldap_add))
187                                        {
188                                                return false;
189                                        }
190                                }
191                        }
192                        /* userPasswordRFC2617 End's*/
193                       
194                        if (!@ldap_modify($ds, $dn, $entry))
195                        {
196                                return false;
197                        }
198                        $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd));
199                        return $new_passwd;
200                }
201
202                function update_lastlogin($_account_id, $ip)
203                {
204                        if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap')
205                        {
206                                $entry['phpgwaccountlastlogin']     = time();
207                                $entry['phpgwaccountlastloginfrom'] = $ip;
208       
209                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
210                                $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], 'uidnumber=' . (int)$_account_id);
211                                $allValues = ldap_get_entries($ds, $sri);
212       
213                                $dn = $allValues[0]['dn'];
214                                $this->previous_login = $allValues[0]['phpgwaccountlastlogin'][0];
215       
216                                @ldap_modify($ds, $dn, $entry);
217                        }
218                        else
219                        {
220                                $GLOBALS['phpgw']->db->query("select account_lastlogin from phpgw_accounts where account_id='$_account_id'",__LINE__,__FILE__);
221                                $GLOBALS['phpgw']->db->next_record();
222                                $this->previous_login = $GLOBALS['phpgw']->db->f('account_lastlogin');
223       
224                                $GLOBALS['phpgw']->db->query("update phpgw_accounts set account_lastloginfrom='"
225                                        . "$ip', account_lastlogin='" . time()
226                                        . "' where account_id='$_account_id'",__LINE__,__FILE__);
227                        }
228                }
229        }
230?>
Note: See TracBrowser for help on using the repository browser.