1 18 19 package sync4j.exchange.security; 20 21 import java.security.Principal ; 22 import java.util.logging.Level ; 23 import java.util.logging.Logger ; 24 25 import sync4j.exchange.DataAccessException; 26 27 import sync4j.exchange.items.common.manager.ItemManager; 28 29 import sync4j.framework.core.Authentication; 30 import sync4j.framework.core.Cred; 31 import sync4j.framework.security.Officer; 32 import sync4j.framework.security.Sync4jPrincipal; 33 import sync4j.framework.server.store.*; 34 import sync4j.framework.tools.Base64; 35 36 import sync4j.server.config.Configuration; 37 38 39 46 public class ExchangeOfficer implements Officer, java.io.Serializable { 47 48 49 51 53 protected Logger log = Logger.getLogger("source"); 54 55 protected PersistentStore ps = null; 56 57 59 61 64 private boolean loginFailed = false; 65 66 public boolean isLoginFailed() { 67 return loginFailed; 68 } 69 70 73 private boolean loginExpired = false; 74 75 78 private String clientAuth = Cred.AUTH_TYPE_BASIC; 79 80 public String getClientAuth() { 81 return this.clientAuth; 82 } 83 84 public void setClientAuth(String clientAuth) { 85 this.clientAuth = clientAuth; 86 } 87 88 91 private String serverAuth = Cred.AUTH_TYPE_BASIC; 92 93 public String getServerAuth() { 94 return this.serverAuth; 95 } 96 97 public void setServerAuth(String serverAuth) { 98 this.serverAuth = serverAuth; 99 } 100 101 104 private String exchangeHost = null; 105 106 public String getExchangeHost() { 107 return this.exchangeHost; 108 } 109 110 public void setExchangeHost(String exchangeHost) { 111 this.exchangeHost = exchangeHost; 112 } 113 114 117 private int exchangePort = 0; 118 119 public int getExchangePort() { 120 return this.exchangePort; 121 } 122 123 public void setExchangePort(int exchangePort) { 124 this.exchangePort = exchangePort; 125 } 126 127 130 private String exchangeFolder = null; 131 132 public String getExchangeFolder() { 133 return this.exchangeFolder; 134 } 135 136 public void setExchangeFolder(String exchangeFolder) { 137 this.exchangeFolder = exchangeFolder; 138 } 139 140 142 149 public boolean authenticate(Cred credential) { 150 151 if (log.isLoggable(Level.FINEST)) { 152 log.finest(" ExchangeOfficer authenticate() Start"); 153 } 154 155 Configuration config = Configuration.getConfiguration(); 156 ps = config.getStore(); 157 158 String type = credential.getType(); 159 160 if ((Cred.AUTH_TYPE_BASIC).equals(type)) { 161 return authenticateBasicCredential(credential); 162 } 163 return false; 164 } 165 166 167 176 public boolean authorize(Principal principal, String resource) { 177 return true; 178 } 179 180 187 public void unAuthenticate(Cred credential) { 188 } 189 190 195 public boolean isAccountExpired() { 196 return loginExpired; 197 } 198 199 201 204 private boolean authenticateBasicCredential(Cred credential) { 205 206 String username = null, password = null; 207 Sync4jPrincipal principal = null; 208 209 Authentication auth = credential.getAuthentication(); 210 String deviceId = auth.getDeviceId(); 211 String credentials = auth.getData(); 212 String userpwd = new String (Base64.decode(auth.getData())); 213 214 int p = userpwd.indexOf(':'); 215 216 if (p == -1) { 217 username = userpwd; 218 password = ""; 219 } else { 220 username = (p > 0) ? userpwd.substring(0, p) : ""; 221 password = (p == (userpwd.length() - 1)) ? "" : 222 userpwd.substring(p + 1); 223 } 224 225 if (log.isLoggable(Level.FINEST)) { 226 log.finest("Username: " + username ); 227 log.finest("Credentials: " + credentials ); 228 } 229 230 boolean principalFound = false; 235 try { 236 principal = new Sync4jPrincipal(username, deviceId); 237 ps.read(principal); 238 credential.getAuthentication().setPrincipalId(principal.getId()); 239 principalFound = true; 240 } catch (NotFoundException nfe) { 241 if (log.isLoggable(Level.FINEST)) { 242 log.finest("Principal for " + username + ":" + deviceId + 243 " not found"); 244 } 245 } catch (PersistentStoreException e) { 246 log.severe("Error reading principal: " + e); 247 log.throwing(getClass().getName(), "authenticateBasicCredential", e); 248 return false; 249 } 250 251 boolean isExchangeUser = checkExchangeCredentials( 255 exchangeHost, 256 exchangePort, 257 exchangeFolder, 258 username, 259 credentials 260 ); 261 262 if (!isExchangeUser) { 263 return false; 267 } 268 269 if (!principalFound) { 270 principal = new Sync4jPrincipal(username, deviceId); 274 try { 275 ps.store(principal); 276 } catch (PersistentStoreException ex) { 277 ex.printStackTrace(); 278 log.severe("Error creating new principal: " + ex); 279 log.throwing(getClass().getName(), 280 "authenticateBasicCredential", ex); 281 return false; 282 } 283 284 credential.getAuthentication().setPrincipalId(principal.getId()); 285 } 286 287 return true; 288 } 289 290 305 private boolean checkExchangeCredentials(String exchangeHost , 306 int exchangePort , 307 String exchangeFolder , 308 String userName , 309 String credentials) { 310 int status = 0; 311 312 if (exchangeHost == null || exchangeHost.equals("")) { 313 314 String message = "'Exchange host' is null or empty. You have to set " + 315 "the Exchange server host in the file: " + 316 "Sync4j/config/sync4j/server/security/ExchangeOfficer.xml"; 317 318 if (log.isLoggable(Level.SEVERE)) { 319 log.severe(message); 320 } 321 322 throw new IllegalStateException (message); 323 } 324 325 326 if (exchangeFolder == null || exchangeFolder.equals("")) { 327 328 String message = "'Exchange folder' is null or empty. You have to set " + 329 "the Exchange folder folder in the file: " + 330 "Sync4j/config/sync4j/server/security/ExchangeOfficer.xml"; 331 332 if (log.isLoggable(Level.SEVERE)) { 333 log.severe(message); 334 } 335 336 throw new IllegalStateException (message); 337 } 338 339 if (exchangeFolder.indexOf("/") == - 1) { 340 341 String message = "'Exchange folder' must be contain '/'. You have to set " + 342 "the Exchange folder host in the file: " + 343 "Sync4j/config/sync4j/server/security/ExchangeOfficer.xml"; 344 345 if (log.isLoggable(Level.SEVERE)) { 346 log.severe(message); 347 } 348 349 throw new IllegalStateException (message); 350 } 351 352 String exchangeServerName 353 = exchangeFolder. 354 substring(0, 355 exchangeFolder.indexOf("/")); 356 357 boolean check = false; 358 359 try { 360 361 ItemManager im = new ItemManager (exchangeHost, 362 exchangePort); 363 364 status = im.getExchangeAccessStatus(exchangeServerName , 365 userName , 366 credentials ); 367 368 if (status == 401 || status == 406) { 369 check = false; 370 } else { 371 check = true; 372 } 373 374 } catch (DataAccessException dae) { 375 log.severe("Error getting connection: " + dae); 376 log.throwing(getClass().getName(), "check Exchange Credentials", 377 dae); 378 } 379 return check; 380 } 381 382 } 383 | Popular Tags |