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 change_password_user ($old_passwd, $new_passwd, $dn, $referrals=false) |
---|
203 | { |
---|
204 | //Use with RHDS |
---|
205 | //system('echo "CP old_passwd: '.$old_passwd.'" >>/tmp/controle'); |
---|
206 | //system('echo "CP new_passwd: '.$new_passwd.'" >>/tmp/controle'); |
---|
207 | //system('echo "CP dn: '.$dn.'" >>/tmp/controle'); |
---|
208 | //system('echo "CP referrals: '.$referrals.'" >>/tmp/controle'); |
---|
209 | $ds=ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']); |
---|
210 | if (!$ds) |
---|
211 | { |
---|
212 | //system('echo "CP Nao conectou no ldap" >>/tmp/controle'); |
---|
213 | $this->auth_reason = ldap_errno($ldap); |
---|
214 | return False; |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | if ($referrals) |
---|
219 | { |
---|
220 | //system('echo "CP Entrou referrals" >>/tmp/controle'); |
---|
221 | $this->passwd=$old_passwd; |
---|
222 | $this->dn=$dn; |
---|
223 | ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); |
---|
224 | ldap_set_option($ds, LDAP_OPT_REFERRALS, 1); |
---|
225 | if($GLOBALS['phpgw_info']['server']['diretorioescravo']) |
---|
226 | { |
---|
227 | ldap_set_rebind_proc($ds, array($this, '_rebindProc')); |
---|
228 | } |
---|
229 | } |
---|
230 | $modify["userpassword"]=$new_passwd; |
---|
231 | if (!@ldap_bind($ds,$dn,$old_passwd)) |
---|
232 | { |
---|
233 | //system('echo "CP nao conseguiu dar bind" >>/tmp/controle'); |
---|
234 | //Se a politica estiver no diretorio eh necessario tentar alterar a senha mesmo que nao haja um bind, pois a negacao de bind pode ser proveniente de uma expiracao |
---|
235 | if($GLOBALS['phpgw_info']['server']['politicasenhas']=='diretorio') |
---|
236 | { |
---|
237 | //system('echo "CP politica eh no diretorio" >>/tmp/controle'); |
---|
238 | if (!@ldap_mod_replace($ds,$dn,$modify)) |
---|
239 | { |
---|
240 | //system('echo "CP nao conseguiu fazer replace!" >>/tmp/controle'); |
---|
241 | $this->auth_reason = ldap_errno($ds); |
---|
242 | return false; |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | //system('echo "CP replace funcionou!" >>/tmp/controle'); |
---|
247 | $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd)); |
---|
248 | return $new_passwd; |
---|
249 | } |
---|
250 | } |
---|
251 | $this->auth_reason = ldap_errno($ds); |
---|
252 | return False; |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | //system('echo "CP Conseguiu dar bind" >>/tmp/controle'); |
---|
257 | if (!ldap_mod_replace($ds,$dn,$modify)) |
---|
258 | { |
---|
259 | $this->auth_reason = ldap_errno($ds); |
---|
260 | return False; |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | $GLOBALS['phpgw']->session->appsession('password','phpgwapi',base64_encode($new_passwd)); |
---|
265 | return $new_passwd; |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | function update_lastlogin($_account_id, $ip) |
---|
273 | { |
---|
274 | if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap') |
---|
275 | { |
---|
276 | $entry['phpgwaccountlastlogin'] = time(); |
---|
277 | $entry['phpgwaccountlastloginfrom'] = $ip; |
---|
278 | |
---|
279 | $ds = $GLOBALS['phpgw']->common->ldapConnect(); |
---|
280 | $sri = ldap_search($ds, $GLOBALS['phpgw_info']['server']['ldap_context'], 'uidnumber=' . (int)$_account_id); |
---|
281 | $allValues = ldap_get_entries($ds, $sri); |
---|
282 | |
---|
283 | $dn = $allValues[0]['dn']; |
---|
284 | $this->previous_login = $allValues[0]['phpgwaccountlastlogin'][0]; |
---|
285 | |
---|
286 | @ldap_modify($ds, $dn, $entry); |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | $GLOBALS['phpgw']->db->query("select account_lastlogin from phpgw_accounts where account_id='$_account_id'",__LINE__,__FILE__); |
---|
291 | $GLOBALS['phpgw']->db->next_record(); |
---|
292 | $this->previous_login = $GLOBALS['phpgw']->db->f('account_lastlogin'); |
---|
293 | |
---|
294 | $GLOBALS['phpgw']->db->query("update phpgw_accounts set account_lastloginfrom='" |
---|
295 | . "$ip', account_lastlogin='" . time() |
---|
296 | . "' where account_id='$_account_id'",__LINE__,__FILE__); |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | ?> |
---|