[1545] | 1 | /******************************************************************************* |
---|
| 2 | * Author: Emerson faria Nobre - emerson-faria.nobre@serpro.gov.br - january/09 |
---|
| 3 | * Organization: SERPRO - Servico Federal de Processamento de Dados |
---|
| 4 | * Description: This source code is an extension of UserProvisioningOfficer.java |
---|
| 5 | * New capabilities implemented: |
---|
| 6 | * - Authenticate user in Ldap. |
---|
| 7 | * - Automatically Create/Update the MailServerAccount |
---|
| 8 | * (tables: fnbl_email_account, fnbl_email_enable_account, |
---|
| 9 | * fnbl_email_push_registry) |
---|
| 10 | * Changes: |
---|
| 11 | * Author/Date/Description: |
---|
| 12 | * Emerson Faria Nobre - june/2009 - Inserted parameters UserFieldName and |
---|
| 13 | * PwdFieldName because the LDAP Server of each Company that I |
---|
| 14 | * need to install Funambol use diferent names for this fields. |
---|
| 15 | * |
---|
| 16 | * Changes: |
---|
| 17 | * Autor/Description/Date: |
---|
| 18 | * Lucas da Costa Silva / Sync with notes, photo, address, and the possibility |
---|
| 19 | * to login with any uid from the overlay and it will use |
---|
| 20 | * only the uid from the ldap not to add multiple principal |
---|
| 21 | * to the same person that use different user in the same |
---|
| 22 | * device. |
---|
| 23 | * / Sep to Oct-2009 |
---|
| 24 | * |
---|
| 25 | ******************************************************************************* |
---|
| 26 | * |
---|
| 27 | * |
---|
| 28 | * |
---|
| 29 | * Funambol is a mobile platform developed by Funambol, Inc. |
---|
| 30 | * Copyright (C) 2006 - 2007 Funambol, Inc. |
---|
| 31 | * |
---|
| 32 | * This program is free software; you can redistribute it and/or modify it under |
---|
| 33 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
| 34 | * the Free Software Foundation with the addition of the following permission |
---|
| 35 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
| 36 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
| 37 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
| 38 | * |
---|
| 39 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 40 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 41 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 42 | * details. |
---|
| 43 | * |
---|
| 44 | * You should have received a copy of the GNU Affero General Public License |
---|
| 45 | * along with this program; if not, see http://www.gnu.org/licenses or write to |
---|
| 46 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
| 47 | * MA 02110-1301 USA. |
---|
| 48 | * |
---|
| 49 | * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite |
---|
| 50 | * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. |
---|
| 51 | * |
---|
| 52 | * The interactive user interfaces in modified source and object code versions |
---|
| 53 | * of this program must display Appropriate Legal Notices, as required under |
---|
| 54 | * Section 5 of the GNU Affero General Public License version 3. |
---|
| 55 | * |
---|
| 56 | * In accordance with Section 7(b) of the GNU Affero General Public License |
---|
| 57 | * version 3, these Appropriate Legal Notices must retain the display of the |
---|
| 58 | * "Powered by Funambol" logo. If the display of the logo is not reasonably |
---|
| 59 | * feasible for technical reasons, the Appropriate Legal Notices must display |
---|
| 60 | * the words "Powered by Funambol". |
---|
| 61 | */ |
---|
| 62 | package com.funambol.server.security; |
---|
| 63 | |
---|
| 64 | import java.util.Hashtable; |
---|
| 65 | import java.util.List; |
---|
| 66 | |
---|
| 67 | import javax.naming.Context; |
---|
| 68 | import javax.naming.NamingEnumeration; |
---|
| 69 | import javax.naming.NamingException; |
---|
| 70 | import javax.naming.directory.DirContext; |
---|
| 71 | import javax.naming.directory.InitialDirContext; |
---|
| 72 | import javax.naming.directory.SearchControls; |
---|
| 73 | import javax.naming.directory.SearchResult; |
---|
| 74 | |
---|
| 75 | import sun.util.logging.resources.logging; |
---|
| 76 | |
---|
| 77 | import com.funambol.email.console.dao.ConsoleDAO; |
---|
| 78 | import com.funambol.email.exception.DBAccessException; |
---|
| 79 | import com.funambol.email.exception.InboxListenerConfigException; |
---|
| 80 | import com.funambol.email.model.MailServer; |
---|
| 81 | import com.funambol.email.model.MailServerAccount; |
---|
| 82 | import com.funambol.email.util.Def; |
---|
| 83 | import com.funambol.framework.core.Authentication; |
---|
| 84 | import com.funambol.framework.core.Cred; |
---|
| 85 | import com.funambol.framework.filter.WhereClause; |
---|
| 86 | import com.funambol.framework.security.Sync4jPrincipal; |
---|
| 87 | import com.funambol.framework.server.Sync4jUser; |
---|
| 88 | import com.funambol.framework.server.store.NotFoundException; |
---|
| 89 | import com.funambol.framework.server.store.PersistentStoreException; |
---|
| 90 | import com.funambol.framework.tools.Base64; |
---|
| 91 | import com.funambol.framework.tools.beans.LazyInitBean; |
---|
| 92 | import com.funambol.pushlistener.service.registry.RegistryEntryStatus; |
---|
| 93 | import com.funambol.server.admin.AdminException; |
---|
| 94 | import com.funambol.server.admin.UserManager; |
---|
| 95 | import com.funambol.server.config.Configuration; |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * This is an implementation of the <i>Officier</i> interface. It provides |
---|
| 100 | * the user provisioning so if an user is not in the database he will be added. |
---|
| 101 | * It requires basic authentication |
---|
| 102 | * |
---|
| 103 | * @version $Id: UserProvisioningOfficer.java,v 1.4 2008-06-24 12:50:06 piter_may Exp $ |
---|
| 104 | */ |
---|
| 105 | public class LdapUserProvisioningOfficer |
---|
| 106 | extends DBOfficer |
---|
| 107 | implements LazyInitBean { |
---|
| 108 | |
---|
| 109 | private static final long serialVersionUID = 1978810349147209602L; |
---|
| 110 | |
---|
| 111 | Ldap objLdap; |
---|
| 112 | MailServerAccount msa; |
---|
| 113 | Boolean InsertMSA; |
---|
| 114 | String ldapIP; |
---|
| 115 | String ldapPort; |
---|
| 116 | String ldapStartSearchPath; |
---|
| 117 | |
---|
| 118 | boolean MsaEnablePush; |
---|
| 119 | boolean MsaEnablePolling; |
---|
| 120 | int MsaRefreshTime; |
---|
| 121 | int MsaMaxEmailNumber; |
---|
| 122 | int MsaMaxImapEmails; |
---|
| 123 | |
---|
| 124 | // ------------------------------------------------------------ Constructors |
---|
| 125 | public LdapUserProvisioningOfficer() { |
---|
| 126 | super(); |
---|
| 127 | msa = new MailServerAccount(); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // ---------------------------------------------------------- Public methods |
---|
| 131 | public void init() { |
---|
| 132 | super.init(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Authenticates a credential. |
---|
| 137 | * |
---|
| 138 | * @param credential the credential to be authenticated |
---|
| 139 | * |
---|
| 140 | * @return the Sync4jUser if the credential is autenticated, null otherwise |
---|
| 141 | */ |
---|
| 142 | public Sync4jUser authenticateUser(Cred credential) { |
---|
| 143 | |
---|
| 144 | if (log.isTraceEnabled()) { |
---|
| 145 | StringBuffer sb = new StringBuffer("##########"); |
---|
| 146 | sb.append(credential.getAuthentication().getPassword()); |
---|
| 147 | sb.append("::"); |
---|
| 148 | sb.append(credential.getAuthentication().getData()); |
---|
| 149 | sb.append("::"); |
---|
| 150 | sb.append(credential.getAuthentication().getUsername()); |
---|
| 151 | sb.append("::"); |
---|
| 152 | sb.append(credential.getUsername()); |
---|
| 153 | sb.append("::"); |
---|
| 154 | sb.append(credential.getData()); |
---|
| 155 | sb.append("::"); |
---|
| 156 | sb.append(credential.getFormat()); |
---|
| 157 | sb.append("::"); |
---|
| 158 | sb.append(credential.getType()); |
---|
| 159 | sb.append("##############"); |
---|
| 160 | log.trace(sb.toString()); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | Configuration config = Configuration.getConfiguration(); |
---|
| 165 | ps = config.getStore(); |
---|
| 166 | |
---|
| 167 | userManager = (UserManager) config.getUserManager(); |
---|
| 168 | |
---|
| 169 | String type = credential.getType(); |
---|
| 170 | |
---|
| 171 | if ((Cred.AUTH_TYPE_BASIC).equals(type)) { |
---|
| 172 | return authenticateBasicCredential(credential,"simple"); |
---|
| 173 | |
---|
| 174 | } else if ((Cred.AUTH_TYPE_MD5).equals(type)) { |
---|
| 175 | return authenticateMD5Credential(credential); |
---|
| 176 | |
---|
| 177 | } |
---|
| 178 | return null; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | protected Sync4jUser getUser(String userName, String password) { |
---|
| 182 | |
---|
| 183 | try { |
---|
| 184 | objLdap = new Ldap(this.getLdapIP(), this.getLdapPort(), userName, |
---|
| 185 | "empty", this.getLdapStartSearchPath(), "nothing is so simple"); |
---|
| 186 | |
---|
| 187 | return super.getUser(objLdap.getUid(),password); |
---|
| 188 | |
---|
| 189 | } catch (Exception e) { |
---|
| 190 | return null; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Gets the supported authentication type |
---|
| 197 | * |
---|
| 198 | * @return the basic authentication type |
---|
| 199 | */ |
---|
| 200 | public String getClientAuth() { |
---|
| 201 | return Cred.AUTH_TYPE_BASIC; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // ------------------------------------------------------- Protected Methods |
---|
| 205 | |
---|
| 206 | // Insert/Update MailServerAccount (MSA) |
---|
| 207 | protected void InsertUpdateMSA(String user, String pwd) { |
---|
| 208 | |
---|
| 209 | if (log.isTraceEnabled()) { |
---|
| 210 | log.trace("LdapUserProvisioningOfficer - I will create cdao object"); |
---|
| 211 | } |
---|
| 212 | try { |
---|
| 213 | ConsoleDAO cdao = new ConsoleDAO(); |
---|
| 214 | |
---|
| 215 | if (log.isTraceEnabled()) { |
---|
| 216 | log.trace("LdapUserProvisioningOfficer - I will verify if it is insert or update"); |
---|
| 217 | } |
---|
| 218 | msa = null; |
---|
| 219 | |
---|
| 220 | if (log.isTraceEnabled()) { |
---|
| 221 | log.trace("LdapUserProvisioningOfficer - before command cdao.getUser(user)"); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | List<MailServerAccount> accounts = cdao.getUserAccounts(user); |
---|
| 225 | if(accounts!=null && accounts.size()>0) { |
---|
| 226 | for (MailServerAccount mailServerAccount : accounts) { |
---|
| 227 | if(mailServerAccount.getMailServer().getDescription().equals("expresso")) { |
---|
| 228 | msa = accounts.get(0); |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if (log.isTraceEnabled()) { |
---|
| 234 | log.trace("LdapUserProvisioningOfficer - after command cdao.getUser(user)"); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (msa == null) { |
---|
| 238 | msa = new MailServerAccount(); |
---|
| 239 | this.InsertMSA = true; |
---|
| 240 | } else { |
---|
| 241 | this.InsertMSA = false; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | if (log.isTraceEnabled()) { |
---|
| 245 | log.trace("LdapUserProvisioningOfficer - after if (msa == null)"); |
---|
| 246 | log.trace("LdapUserProvisioningOfficer - msa.getUsername() = " + msa.getUsername()); |
---|
| 247 | log.trace("LdapUserProvisioningOfficer - It is insert: " + this.InsertMSA + "- username = " + msa.getUsername()); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | if (!this.InsertMSA) { |
---|
| 251 | if (log.isTraceEnabled()) { |
---|
| 252 | log.trace("LdapUserProvisioningOfficer - The key Id = " + msa.getId()); |
---|
| 253 | log.trace("LdapUserProvisioningOfficer - The key UserName = " + msa.getUsername()); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | if (this.InsertMSA) { |
---|
| 257 | if (log.isTraceEnabled()) { |
---|
| 258 | log.trace("LdapUserProvisioningOfficer - I will set setUserName" + user); |
---|
| 259 | } |
---|
| 260 | msa.setUsername(user); |
---|
| 261 | } |
---|
| 262 | msa.setMsLogin(user); |
---|
| 263 | msa.setMsPassword(pwd); |
---|
| 264 | msa.setMsAddress(objLdap.getmail()); |
---|
| 265 | msa.setPush(this.getMsaEnablePush()); |
---|
| 266 | msa.setMaxEmailNumber(this.getMsaMaxEmailNumber()); |
---|
| 267 | msa.setMaxImapEmail(this.getMsaMaxImapEmails()); |
---|
| 268 | msa.setPeriod(this.getMsaRefreshTime()); |
---|
| 269 | msa.setActive(this.getMsaEnablePolling()); |
---|
| 270 | msa.setTaskBeanFile(Def.DEFAULT_INBOX_LISTENER_BEAN_FILE); |
---|
| 271 | msa.setLastUpdate(System.currentTimeMillis()); |
---|
| 272 | |
---|
| 273 | if (this.InsertMSA) { |
---|
| 274 | msa.setStatus(RegistryEntryStatus.NEW); |
---|
| 275 | } else { |
---|
| 276 | msa.setStatus(RegistryEntryStatus.UPDATED); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | if (log.isTraceEnabled()) { |
---|
| 280 | log.trace("LdapUserProvisioningOfficer - I already set setMsLogin" + user); |
---|
| 281 | log.trace("LdapUserProvisioningOfficer - I will set setMsPassword"); |
---|
| 282 | log.trace("LdapUserProvisioningOfficer - I will set setMsAddress = " + objLdap.getmail()); |
---|
| 283 | log.trace("LdapUserProvisioningOfficer - I will set setPush = " + this.getMsaEnablePush()); |
---|
| 284 | log.trace("LdapUserProvisioningOfficer - I will set setMaxEmailNumber = " + this.getMsaMaxEmailNumber()); |
---|
| 285 | log.trace("LdapUserProvisioningOfficer - I will set setMaxImapEmail = " + this.getMsaMaxImapEmails()); |
---|
| 286 | log.trace("LdapUserProvisioningOfficer - I will set setPeriod = " + this.getMsaRefreshTime()); |
---|
| 287 | log.trace("LdapUserProvisioningOfficer - I will set setActive = " + this.getMsaEnablePolling()); |
---|
| 288 | log.trace("LdapUserProvisioningOfficer - I will set fnbl_email_push_registry"); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | String[] param = {"description"}; |
---|
| 292 | String[] value = {"expresso"}; |
---|
| 293 | String[] operator = {WhereClause.OPT_EQ}; |
---|
| 294 | |
---|
| 295 | MailServer[] ms = cdao.getPubMailServers(new WhereClause(param[0], new String[]{value[0]}, operator[0], false)); |
---|
| 296 | |
---|
| 297 | if (log.isTraceEnabled()) { |
---|
| 298 | log.trace("LdapUserProvisioningOfficer - I found the MailServer expresso - ID = " + ms[0].getMailServerId()); |
---|
| 299 | } |
---|
| 300 | msa.setMailServer(cdao.getPubMailServer(ms[0].getMailServerId())); |
---|
| 301 | |
---|
| 302 | if (this.InsertMSA) { |
---|
| 303 | int ret = cdao.insertUserAccount(msa); |
---|
| 304 | if (log.isTraceEnabled()) { |
---|
| 305 | log.trace("LdapUserProvisioningOfficer - return of insertUser(msa): " + ret); |
---|
| 306 | } |
---|
| 307 | } else { |
---|
| 308 | int ret = cdao.updateUserAccount(msa); |
---|
| 309 | if (log.isTraceEnabled()) { |
---|
| 310 | log.trace("LdapUserProvisioningOfficer - return of updateUser(msa): " + ret); |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | } catch (InboxListenerConfigException e) { |
---|
| 314 | log.error("LdapUserProvisioningOfficer - Error creating DAO layer to Insert/Update Mail User: ", e); |
---|
| 315 | } catch (DBAccessException e) { |
---|
| 316 | log.error("LdapUserProvisioningOfficer - Error accessing Database to Insert/Update Mail User: ", e); |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | /** |
---|
| 321 | * Checks the given credential. If the user or the principal isn't found, |
---|
| 322 | * they are created. |
---|
| 323 | * |
---|
| 324 | * @param credential the credential to check |
---|
| 325 | * |
---|
| 326 | * @return the Sync4jUser if the credential is autenticated, null otherwise |
---|
| 327 | */ |
---|
| 328 | protected Sync4jUser authenticateBasicCredential(Cred credential, String authType) { |
---|
| 329 | String username = null, password = null; |
---|
| 330 | |
---|
| 331 | Authentication auth = credential.getAuthentication(); |
---|
| 332 | String deviceId = auth.getDeviceId(); |
---|
| 333 | |
---|
| 334 | String userpwd = new String(Base64.decode(auth.getData())); |
---|
| 335 | |
---|
| 336 | int p = userpwd.indexOf(':'); |
---|
| 337 | |
---|
| 338 | if (p == -1) { |
---|
| 339 | username = userpwd; |
---|
| 340 | password = ""; |
---|
| 341 | } else { |
---|
| 342 | username = (p > 0) ? userpwd.substring(0, p) : ""; |
---|
| 343 | password = (p == (userpwd.length() - 1)) ? "" : userpwd.substring(p + 1); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if (log.isTraceEnabled()) { |
---|
| 347 | log.trace("User to check: " + username); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | // LDAP Checkpoint |
---|
| 351 | // |
---|
| 352 | if (log.isTraceEnabled()) { |
---|
| 353 | log.trace("LdapUserProvisioningOfficer - CheckPoint LDAP - getLdapIP: " + this.getLdapIP() + " getLdapPort: " + this.getLdapPort()); |
---|
| 354 | } |
---|
| 355 | objLdap = new Ldap(this.getLdapIP(), this.getLdapPort(), username, password, this.getLdapStartSearchPath(), authType); |
---|
| 356 | |
---|
| 357 | // Try to authenticate in LDAP |
---|
| 358 | if (objLdap.getreturnStatus() == false) { |
---|
| 359 | if (log.isTraceEnabled()) { |
---|
| 360 | log.trace("LDAP Authentication Failure: " + objLdap.geterrorMsg() + " - " + objLdap.geterrorStatus()); |
---|
| 361 | } |
---|
| 362 | return null; |
---|
| 363 | } |
---|
| 364 | this.InsertUpdateMSA(objLdap.getUid(), password); |
---|
| 365 | |
---|
| 366 | // Gets the user |
---|
| 367 | Sync4jUser user = getUser(objLdap.getUid(), null); |
---|
| 368 | if (user == null) { |
---|
| 369 | try { |
---|
| 370 | user = insertUser(objLdap.getUid(), password); |
---|
| 371 | if (log.isTraceEnabled()) { |
---|
| 372 | log.trace(username+" to User '" + objLdap.getUid() + "' created"); |
---|
| 373 | } |
---|
| 374 | } catch (Exception e) { |
---|
| 375 | log.error("Error inserting a new user", e); |
---|
| 376 | return null; |
---|
| 377 | } |
---|
| 378 | } else { |
---|
| 379 | if (log.isTraceEnabled()) { |
---|
| 380 | log.trace(username+" to User '" + objLdap.getUid() + "' found"); |
---|
| 381 | } |
---|
| 382 | // Check the roles |
---|
| 383 | // |
---|
| 384 | if (isASyncUser(user)) { |
---|
| 385 | // |
---|
| 386 | // User authenticated |
---|
| 387 | if (log.isTraceEnabled()) { |
---|
| 388 | log.trace("User is a SyncUser"); |
---|
| 389 | } |
---|
| 390 | } else { |
---|
| 391 | // User not authenticated |
---|
| 392 | // |
---|
| 393 | if (log.isTraceEnabled()) { |
---|
| 394 | log.trace("The user is not a '" + ROLE_USER + "'"); |
---|
| 395 | } |
---|
| 396 | return null; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | // |
---|
| 401 | // Verify that the principal for the specify deviceId and username exists |
---|
| 402 | // Otherwise a new principal will be created |
---|
| 403 | // |
---|
| 404 | try { |
---|
| 405 | handlePrincipal(objLdap.getUid(), deviceId); |
---|
| 406 | } catch (PersistentStoreException e) { |
---|
| 407 | log.error("Error handling the principal", e); |
---|
| 408 | return null; |
---|
| 409 | } |
---|
| 410 | return user; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | /** |
---|
| 414 | * Insert a new user with the given username and password |
---|
| 415 | * |
---|
| 416 | * @param userName the username |
---|
| 417 | * @param password the password |
---|
| 418 | * |
---|
| 419 | * @return the new user |
---|
| 420 | * |
---|
| 421 | * @throws AdminException in case of admin errors |
---|
| 422 | * @throws PersistentStoreException if an error occurs |
---|
| 423 | */ |
---|
| 424 | protected Sync4jUser insertUser( |
---|
| 425 | String userName, String password) |
---|
| 426 | throws AdminException, PersistentStoreException { |
---|
| 427 | |
---|
| 428 | Sync4jUser user = new Sync4jUser(); |
---|
| 429 | user.setUsername(userName); |
---|
| 430 | user.setPassword(password); |
---|
| 431 | user.setFirstname(objLdap.getcn()); |
---|
| 432 | user.setRoles(new String[]{ROLE_USER}); |
---|
| 433 | user.setEmail(objLdap.getmail()); |
---|
| 434 | |
---|
| 435 | userManager.insertUser(user); |
---|
| 436 | return user; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | /** |
---|
| 440 | * Returns the principal with the given username and deviceId. |
---|
| 441 | * <code>null</code> if not found |
---|
| 442 | * @param userName the username |
---|
| 443 | * @param deviceId the device id |
---|
| 444 | * @return the principal found or null. |
---|
| 445 | * @throws PersistentStoreException if an error occurs |
---|
| 446 | */ |
---|
| 447 | protected Sync4jPrincipal getPrincipal(String userName, String deviceId) |
---|
| 448 | throws PersistentStoreException { |
---|
| 449 | |
---|
| 450 | Sync4jPrincipal principal = null; |
---|
| 451 | |
---|
| 452 | // |
---|
| 453 | // Verify that exist the principal for the specify deviceId and username |
---|
| 454 | // |
---|
| 455 | principal = Sync4jPrincipal.createPrincipal(userName, deviceId); |
---|
| 456 | |
---|
| 457 | try { |
---|
| 458 | ps.read(principal); |
---|
| 459 | } catch (NotFoundException ex) { |
---|
| 460 | return null; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | return principal; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /** |
---|
| 467 | * Inserts a new principal with the given userName and deviceId |
---|
| 468 | * @param userName the username |
---|
| 469 | * @param deviceId the device id |
---|
| 470 | * @return the principal created |
---|
| 471 | * @throws PersistentStoreException if an error occurs creating the principal |
---|
| 472 | */ |
---|
| 473 | protected Sync4jPrincipal insertPrincipal(String userName, String deviceId) |
---|
| 474 | throws PersistentStoreException { |
---|
| 475 | |
---|
| 476 | // |
---|
| 477 | // We must create a new principal |
---|
| 478 | // |
---|
| 479 | Sync4jPrincipal principal = Sync4jPrincipal.createPrincipal(userName, deviceId); |
---|
| 480 | |
---|
| 481 | ps.store(principal); |
---|
| 482 | |
---|
| 483 | return principal; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | /** |
---|
| 487 | * Searchs if there is a principal with the given username and device id. |
---|
| 488 | * if no principal is found, a new one is created. |
---|
| 489 | * @param userName the user name |
---|
| 490 | * @param deviceId the device id |
---|
| 491 | * @return the found principal or the new one |
---|
| 492 | */ |
---|
| 493 | protected Sync4jPrincipal handlePrincipal(String username, String deviceId) |
---|
| 494 | throws PersistentStoreException { |
---|
| 495 | |
---|
| 496 | Sync4jPrincipal principal = null; |
---|
| 497 | |
---|
| 498 | // |
---|
| 499 | // Verify if the principal for the specify deviceId and username exists |
---|
| 500 | // |
---|
| 501 | principal = getPrincipal(username, deviceId); |
---|
| 502 | |
---|
| 503 | if (log.isTraceEnabled()) { |
---|
| 504 | log.trace("Principal '" + username + |
---|
| 505 | "/" + |
---|
| 506 | deviceId + "' " + |
---|
| 507 | ((principal != null) ? "found" : "not found. A new principal will be created")); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | if (principal == null) { |
---|
| 511 | principal = insertPrincipal(username, deviceId); |
---|
| 512 | if (log.isTraceEnabled()) { |
---|
| 513 | log.trace("Principal '" + username + |
---|
| 514 | "/" + |
---|
| 515 | deviceId + "' created"); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | return principal; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | public void setLdapIP(String pldapIP) { |
---|
| 524 | this.ldapIP = pldapIP; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | public String getLdapIP() { |
---|
| 528 | return this.ldapIP; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | public void setLdapPort(String pLdapPort) { |
---|
| 532 | this.ldapPort = pLdapPort; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | public String getLdapPort() { |
---|
| 536 | return this.ldapPort; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | public void setLdapStartSearchPath(String pLdapStartSearchPath) { |
---|
| 540 | this.ldapStartSearchPath = pLdapStartSearchPath; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | public String getLdapStartSearchPath() { |
---|
| 544 | return this.ldapStartSearchPath; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | public void setMsaEnablePush(boolean pMsaEnablePush) { |
---|
| 548 | this.MsaEnablePush = pMsaEnablePush; |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | public boolean getMsaEnablePush() { |
---|
| 552 | return this.MsaEnablePush; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | public void setMsaEnablePolling(boolean pMsaEnablePolling) { |
---|
| 556 | this.MsaEnablePolling = pMsaEnablePolling; |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | public boolean getMsaEnablePolling() { |
---|
| 560 | return this.MsaEnablePolling; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | public void setMsaRefreshTime(int pMsaRefreshTime) { |
---|
| 564 | this.MsaRefreshTime = pMsaRefreshTime; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | public int getMsaRefreshTime() { |
---|
| 568 | return this.MsaRefreshTime; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | public void setMsaMaxEmailNumber(int pMsaMaxEmailNumber) { |
---|
| 572 | this.MsaMaxEmailNumber = pMsaMaxEmailNumber; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | public int getMsaMaxEmailNumber() { |
---|
| 576 | return this.MsaMaxEmailNumber; |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | public void setMsaMaxImapEmails(int pMsaMaxImapEmails) { |
---|
| 580 | this.MsaMaxImapEmails = pMsaMaxImapEmails; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | public int getMsaMaxImapEmails() { |
---|
| 584 | return this.MsaMaxImapEmails; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | // Class to manage LDAP |
---|
| 589 | class Ldap { |
---|
| 590 | |
---|
| 591 | private String ldapServer; |
---|
| 592 | private String ldapPort; |
---|
| 593 | private String ldapSearchPath; |
---|
| 594 | private String UserID; |
---|
| 595 | private String userDN; |
---|
| 596 | private String pwd; |
---|
| 597 | private String cn; |
---|
| 598 | private String mail; |
---|
| 599 | private String errorMsg; |
---|
| 600 | private String errorStatus; |
---|
| 601 | private boolean returnStatus; |
---|
| 602 | private String uid; |
---|
| 603 | private String authtype; |
---|
| 604 | |
---|
| 605 | public Ldap(String ldapServer, String ldapPort, String UserID, String pwd, String ldapSearchPath, String authType) { |
---|
| 606 | this.ldapServer = ldapServer; |
---|
| 607 | this.ldapPort = ldapPort; |
---|
| 608 | this.UserID = UserID; |
---|
| 609 | this.pwd = pwd; |
---|
| 610 | this.ldapSearchPath = ldapSearchPath; |
---|
| 611 | this.authtype = authType; |
---|
| 612 | this.returnStatus = processLDAP(); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | private boolean processLDAP() { |
---|
| 616 | // Password cannot be null |
---|
| 617 | if ((pwd.trim().length()) == 0) { |
---|
| 618 | this.errorMsg = "Password Cannot be null"; |
---|
| 619 | this.errorStatus = "nullPwd"; |
---|
| 620 | return false; |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | // Connecting as anonymous to get information about the user |
---|
| 624 | Hashtable<String,String> env = new Hashtable<String,String>(); |
---|
| 625 | env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); |
---|
| 626 | env.put(Context.PROVIDER_URL, "ldap://" + this.ldapServer + ":" + this.ldapPort); |
---|
| 627 | |
---|
| 628 | try { |
---|
| 629 | // Connecting |
---|
| 630 | DirContext ctx = new InitialDirContext(env); |
---|
| 631 | // Searching User |
---|
| 632 | SearchControls ctls = new SearchControls(); |
---|
| 633 | ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); |
---|
| 634 | ctls.setCountLimit(1); |
---|
| 635 | ctls.setTimeLimit(10000); // max 10 seconds |
---|
| 636 | String filter = "(uid=" + this.UserID + ")"; |
---|
| 637 | NamingEnumeration<SearchResult> answer = ctx.search(this.getLdapSearchPath(), filter, ctls); |
---|
| 638 | |
---|
| 639 | if (answer.hasMore()) { // just the first one |
---|
| 640 | SearchResult sr = (SearchResult) answer.next(); |
---|
| 641 | this.userDN = sr.getName() + "," + this.getLdapSearchPath(); |
---|
| 642 | try { |
---|
| 643 | |
---|
| 644 | // Getting User attributes |
---|
| 645 | this.cn = sr.getAttributes().get("cn").get(0).toString(); |
---|
| 646 | this.mail = sr.getAttributes().get("mail").get(0).toString(); |
---|
| 647 | this.uid = sr.getAttributes().get("uid").get(0).toString(); |
---|
| 648 | |
---|
| 649 | } catch (Exception e) { |
---|
| 650 | e.printStackTrace(); |
---|
| 651 | this.errorMsg = e.toString(); |
---|
| 652 | this.errorStatus = "notGetAttributes"; |
---|
| 653 | return false; |
---|
| 654 | } |
---|
| 655 | } else { |
---|
| 656 | this.errorMsg = "User/Password not Found in LDAP"; |
---|
| 657 | this.errorStatus = "notFound"; |
---|
| 658 | return false; |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | } catch (NamingException e) { |
---|
| 662 | e.printStackTrace(); |
---|
| 663 | this.errorMsg = e.toString(); |
---|
| 664 | this.errorStatus = "notConnect"; |
---|
| 665 | return false; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | // only simple auth is suported in ldap |
---|
| 669 | if("simple".equals(authtype)) { |
---|
| 670 | // Binding (Verifing Credentials) |
---|
| 671 | Hashtable<String,String> env2 = new Hashtable<String,String>(); |
---|
| 672 | env2.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); |
---|
| 673 | env2.put(Context.PROVIDER_URL, "ldap://" + this.ldapServer + ":" + this.ldapPort); |
---|
| 674 | env2.put(Context.SECURITY_AUTHENTICATION, authtype); |
---|
| 675 | env2.put(Context.SECURITY_PRINCIPAL, this.userDN); // specify the user dn |
---|
| 676 | env2.put(Context.SECURITY_CREDENTIALS, this.pwd); // specify the password |
---|
| 677 | |
---|
| 678 | try { |
---|
| 679 | DirContext ctx = new InitialDirContext(env2); |
---|
| 680 | ctx.close(); |
---|
| 681 | } catch (NamingException e) { |
---|
| 682 | e.printStackTrace(); |
---|
| 683 | this.errorMsg = e.toString(); |
---|
| 684 | this.errorStatus = "notBind"; |
---|
| 685 | return false; |
---|
| 686 | } |
---|
| 687 | } |
---|
| 688 | return true; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | // Getters methods |
---|
| 692 | public String getldapServer() { |
---|
| 693 | return this.ldapServer; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | public String getldapPort() { |
---|
| 697 | return this.ldapPort; |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | public String getUserID() { |
---|
| 701 | return this.UserID; |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | public String getuserDN() { |
---|
| 705 | return this.userDN; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | public String getcn() { |
---|
| 709 | return this.cn; |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | public String getmail() { |
---|
| 713 | return this.mail; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | public String geterrorMsg() { |
---|
| 717 | return this.errorMsg; |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | public String geterrorStatus() { |
---|
| 721 | return this.errorStatus; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | public boolean getreturnStatus() { |
---|
| 725 | return this.returnStatus; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | public String getLdapSearchPath() { |
---|
| 729 | return this.ldapSearchPath; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | public String getUid() { |
---|
| 733 | return this.uid; |
---|
| 734 | } |
---|
| 735 | } |
---|