1 13 14 package org.ejbca.core.ejb.ra.userdatasource; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Date ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Random ; 23 24 import javax.ejb.CreateException ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.FinderException ; 27 28 import org.ejbca.core.ejb.BaseSessionBean; 29 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal; 30 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome; 31 import org.ejbca.core.ejb.log.ILogSessionLocal; 32 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 33 import org.ejbca.core.model.InternalResources; 34 import org.ejbca.core.model.authorization.AuthorizationDeniedException; 35 import org.ejbca.core.model.authorization.AvailableAccessRules; 36 import org.ejbca.core.model.log.Admin; 37 import org.ejbca.core.model.log.LogEntry; 38 import org.ejbca.core.model.ra.userdatasource.BaseUserDataSource; 39 import org.ejbca.core.model.ra.userdatasource.UserDataSourceConnectionException; 40 import org.ejbca.core.model.ra.userdatasource.UserDataSourceException; 41 import org.ejbca.core.model.ra.userdatasource.UserDataSourceExistsException; 42 43 44 103 public class LocalUserDataSourceSessionBean extends BaseSessionBean { 104 105 106 private static final InternalResources intres = InternalResources.getInstance(); 107 108 111 private UserDataSourceDataLocalHome userdatasourcehome = null; 112 113 116 private IAuthorizationSessionLocal authorizationsession = null; 117 118 121 private ILogSessionLocal logsession = null; 122 123 124 129 public void ejbCreate() throws CreateException { 130 userdatasourcehome = (UserDataSourceDataLocalHome) getLocator().getLocalHome(UserDataSourceDataLocalHome.COMP_NAME); 131 } 132 133 134 139 private ILogSessionLocal getLogSession() { 140 if (logsession == null) { 141 try { 142 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 143 logsession = logsessionhome.create(); 144 } catch (CreateException e) { 145 throw new EJBException (e); 146 } 147 } 148 return logsession; 149 } 151 152 157 private IAuthorizationSessionLocal getAuthorizationSession() { 158 if (authorizationsession == null) { 159 try { 160 IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME); 161 authorizationsession = authorizationsessionhome.create(); 162 } catch (CreateException e) { 163 throw new EJBException (e); 164 } 165 } 166 return authorizationsession; 167 } 169 170 171 172 173 182 public Collection fetch(Admin admin, Collection userdatasourceids, String searchstring) throws UserDataSourceException{ 183 Iterator iter = userdatasourceids.iterator(); 184 ArrayList result = new ArrayList (); 185 while (iter.hasNext()) { 186 Integer id = (Integer ) iter.next(); 187 try { 188 UserDataSourceDataLocal pdl = userdatasourcehome.findByPrimaryKey(id); 189 BaseUserDataSource userdatasource = pdl.getUserDataSource(); 190 if(isAuthorizedToUserDataSource(admin,userdatasource)){ 191 try { 192 result.addAll(pdl.getUserDataSource().fetchUserDataSourceVOs(admin,searchstring)); 193 String msg = intres.getLocalizedMessage("userdatasource.fetcheduserdatasource", pdl.getName()); 194 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, 195 null, LogEntry.EVENT_INFO_USERDATAFETCHED,msg); 196 } catch (UserDataSourceException pe) { 197 String msg = intres.getLocalizedMessage("userdatasource.errorfetchuserdatasource", pdl.getName()); 198 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, 199 null, LogEntry.EVENT_ERROR_USERDATAFETCHED,msg); 200 throw pe; 201 202 } 203 }else{ 204 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Error, not authorized to user data source :" + pdl.getName()); 205 } 206 } catch (FinderException fe) { 207 String msg = intres.getLocalizedMessage("userdatasource.erroruserdatasourceexist", id); 208 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, 209 LogEntry.EVENT_ERROR_USERDATAFETCHED, msg); 210 throw new UserDataSourceException(msg); 211 212 } 213 } 214 215 return result; 216 } 217 218 219 220 227 public void testConnection(Admin admin, int userdatasourceid) throws UserDataSourceConnectionException { 228 debug(">testConnection(id: " + userdatasourceid + ")"); 229 try { 230 UserDataSourceDataLocal pdl = userdatasourcehome.findByPrimaryKey(new Integer (userdatasourceid)); 231 BaseUserDataSource userdatasource = pdl.getUserDataSource(); 232 if(isAuthorizedToEditUserDataSource(admin,userdatasource)){ 233 try { 234 userdatasource.testConnection(admin); 235 String msg = intres.getLocalizedMessage("userdatasource.testedcon", pdl.getName()); 236 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, 237 null, LogEntry.EVENT_INFO_USERDATASOURCEDATA,msg); 238 } catch (UserDataSourceConnectionException pe) { 239 String msg = intres.getLocalizedMessage("userdatasource.errortestcon", pdl.getName()); 240 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, 241 LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg, pe); 242 throw pe; 243 } 244 }else{ 245 String msg = intres.getLocalizedMessage("userdatasource.errortestconauth", pdl.getName()); 246 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 247 } 248 } catch (FinderException fe) { 249 String msg = intres.getLocalizedMessage("userdatasource.erroruserdatasourceexist", new Integer (userdatasourceid)); 250 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, 251 LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg); 252 } 253 debug("<testConnection(id: " + userdatasourceid + ")"); 254 } 255 256 263 264 public void addUserDataSource(Admin admin, String name, BaseUserDataSource userdatasource) throws UserDataSourceExistsException { 265 debug(">addUserDataSource(name: " + name + ")"); 266 addUserDataSource(admin,findFreeUserDataSourceId().intValue(),name,userdatasource); 267 debug("<addUserDataSource()"); 268 } 270 271 279 280 public void addUserDataSource(Admin admin, int id, String name, BaseUserDataSource userdatasource) throws UserDataSourceExistsException { 281 debug(">addUserDataSource(name: " + name + ", id: " + id + ")"); 282 boolean success = false; 283 if(isAuthorizedToEditUserDataSource(admin,userdatasource)){ 284 try { 285 userdatasourcehome.findByName(name); 286 } catch (FinderException e) { 287 try { 288 userdatasourcehome.findByPrimaryKey(new Integer (id)); 289 } catch (FinderException f) { 290 try { 291 userdatasourcehome.create(new Integer (id), name, userdatasource); 292 success = true; 293 } catch (CreateException g) { 294 error("Unexpected error creating new user data source: ", g); 295 } 296 } 297 } 298 if (success) { 299 String msg = intres.getLocalizedMessage("userdatasource.addedsource", name); 300 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg); 301 } else { 302 String msg = intres.getLocalizedMessage("userdatasource.erroraddsource", name); 303 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg); 304 } 305 if (!success) 306 throw new UserDataSourceExistsException(); 307 }else{ 308 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", name); 309 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 310 } 311 debug("<addUserDataSource()"); 312 } 314 320 321 public void changeUserDataSource(Admin admin, String name, BaseUserDataSource userdatasource) { 322 debug(">changeUserDataSource(name: " + name + ")"); 323 boolean success = false; 324 if(isAuthorizedToEditUserDataSource(admin,userdatasource)){ 325 try { 326 UserDataSourceDataLocal htp = userdatasourcehome.findByName(name); 327 htp.setUserDataSource(userdatasource); 328 success = true; 329 } catch (FinderException e) { 330 } 331 332 if (success) { 333 String msg = intres.getLocalizedMessage("userdatasource.changedsource", name); 334 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg); 335 } else { 336 String msg = intres.getLocalizedMessage("userdatasource.errorchangesource", name); 337 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg); 338 } 339 }else{ 340 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", name); 341 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 342 } 343 344 345 debug("<changeUserDataSource()"); 346 } 348 356 public void cloneUserDataSource(Admin admin, String oldname, String newname) throws UserDataSourceExistsException { 357 debug(">cloneUserDataSource(name: " + oldname + ")"); 358 BaseUserDataSource userdatasourcedata = null; 359 try { 360 UserDataSourceDataLocal htp = userdatasourcehome.findByName(oldname); 361 userdatasourcedata = (BaseUserDataSource) htp.getUserDataSource().clone(); 362 if(isAuthorizedToEditUserDataSource(admin,userdatasourcedata)){ 363 try { 364 addUserDataSource(admin, newname, userdatasourcedata); 365 String msg = intres.getLocalizedMessage("userdatasource.clonedsource", newname, oldname); 366 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg); 367 } catch (UserDataSourceExistsException f) { 368 String msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname); 369 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg); 370 throw f; 371 } 372 }else{ 373 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", oldname); 374 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 375 } 376 } catch (FinderException e) { 377 String msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname); 378 error(msg, e); 379 throw new EJBException (e); 380 } catch (CloneNotSupportedException e) { 381 String msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname); 382 error(msg, e); 383 throw new EJBException (e); 384 } 385 386 debug("<cloneUserDataSource()"); 387 } 389 395 public boolean removeUserDataSource(Admin admin, String name) { 396 debug(">removeUserDataSource(name: " + name + ")"); 397 boolean retval = false; 398 try { 399 UserDataSourceDataLocal htp = userdatasourcehome.findByName(name); 400 BaseUserDataSource userdatasource = htp.getUserDataSource(); 401 if(isAuthorizedToEditUserDataSource(admin,userdatasource)){ 402 htp.remove(); 403 String msg = intres.getLocalizedMessage("userdatasource.removedsource", name); 404 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg); 405 retval = true; 406 }else{ 407 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", name); 408 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 409 } 410 } catch (Exception e) { 411 String msg = intres.getLocalizedMessage("userdatasource.errorremovesource", name); 412 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg, e); 413 } 414 debug("<removeUserDataSource()"); 415 416 return retval; 417 } 419 426 public void renameUserDataSource(Admin admin, String oldname, String newname) throws UserDataSourceExistsException { 427 debug(">renameUserDataSource(from " + oldname + " to " + newname + ")"); 428 boolean success = false; 429 try { 430 userdatasourcehome.findByName(newname); 431 } catch (FinderException e) { 432 try { 433 UserDataSourceDataLocal htp = userdatasourcehome.findByName(oldname); 434 if(isAuthorizedToEditUserDataSource(admin,htp.getUserDataSource())){ 435 htp.setName(newname); 436 success = true; 437 }else{ 438 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", oldname); 439 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, msg); 440 } 441 } catch (FinderException g) { 442 } 443 } 444 445 if (success) { 446 String msg = intres.getLocalizedMessage("userdatasource.renamedsource", oldname, newname); 447 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg); 448 } else { 449 String msg = intres.getLocalizedMessage("userdatasource.errorrenamesource", oldname, newname); 450 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg); 451 } 452 453 if (!success) 454 throw new UserDataSourceExistsException(); 455 debug("<renameUserDataSource()"); 456 } 458 465 public Collection getAuthorizedUserDataSourceIds(Admin admin, boolean includeAnyCA) { 466 HashSet returnval = new HashSet (); 467 Collection result = null; 468 boolean superadmin = false; 469 try { 471 try{ 472 superadmin = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR); 473 }catch (AuthorizationDeniedException e1) { 474 log.debug("AuthorizationDeniedException: ", e1); 475 } 476 Collection authorizedcas = this.getAuthorizationSession().getAuthorizedCAIds(admin); 477 result = this.userdatasourcehome.findAll(); 478 Iterator i = result.iterator(); 479 while (i.hasNext()) { 480 UserDataSourceDataLocal next = (UserDataSourceDataLocal) i.next(); 481 if(superadmin){ 482 returnval.add(next.getId()); 483 }else{ 484 BaseUserDataSource userdatasource = next.getUserDataSource(); 485 if(userdatasource.getApplicableCAs().contains(new Integer (BaseUserDataSource.ANYCA))){ 486 if(includeAnyCA){ 487 returnval.add(next.getId()); 488 } 489 }else{ 490 if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){ 491 returnval.add(next.getId()); 492 } 493 } 494 } 495 } 496 } catch (FinderException fe) { 497 String msg = intres.getLocalizedMessage("userdatasource.errorfindingall"); 498 log.error(msg, fe); 499 } 500 501 return returnval; 502 } 504 510 public HashMap getUserDataSourceIdToNameMap(Admin admin) { 511 HashMap returnval = new HashMap (); 512 Collection result = null; 513 514 try { 515 result = userdatasourcehome.findAll(); 516 Iterator i = result.iterator(); 517 while (i.hasNext()) { 518 UserDataSourceDataLocal next = (UserDataSourceDataLocal) i.next(); 519 returnval.put(next.getId(), next.getName()); 520 } 521 } catch (FinderException e) { 522 } 523 return returnval; 524 } 526 527 533 public BaseUserDataSource getUserDataSource(Admin admin, String name) { 534 BaseUserDataSource returnval = null; 535 536 try { 537 BaseUserDataSource result = (userdatasourcehome.findByName(name)).getUserDataSource(); 538 if(isAuthorizedToEditUserDataSource(admin,result)){ 539 returnval = result; 540 }else{ 541 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", name); 542 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 543 } 544 } catch (FinderException e) { 545 } 547 return returnval; 548 } 550 556 public BaseUserDataSource getUserDataSource(Admin admin, int id) { 557 BaseUserDataSource returnval = null; 558 559 try { 560 BaseUserDataSource result = (userdatasourcehome.findByPrimaryKey(new Integer (id))).getUserDataSource(); 561 if(isAuthorizedToEditUserDataSource(admin,result)){ 562 returnval = result; 563 }else{ 564 String msg = intres.getLocalizedMessage("userdatasource.errornotauth", new Integer (id)); 565 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date (),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg); 566 } 567 } catch (FinderException e) { 568 } 570 return returnval; 571 } 573 580 581 public int getUserDataSourceUpdateCount(Admin admin, int userdatasourceid) { 582 int returnval = 0; 583 584 try { 585 returnval = (userdatasourcehome.findByPrimaryKey(new Integer (userdatasourceid))).getUpdateCounter(); 586 } catch (FinderException e) { 587 } 588 589 return returnval; 590 } 591 592 593 600 public int getUserDataSourceId(Admin admin, String name) { 601 int returnval = 0; 602 603 try { 604 Integer id = (userdatasourcehome.findByName(name)).getId(); 605 returnval = id.intValue(); 606 } catch (FinderException e) { 607 } 608 609 return returnval; 610 } 612 620 public String getUserDataSourceName(Admin admin, int id) { 621 debug(">getUserDataSourceName(id: " + id + ")"); 622 String returnval = null; 623 UserDataSourceDataLocal htp = null; 624 try { 625 htp = userdatasourcehome.findByPrimaryKey(new Integer (id)); 626 if (htp != null) { 627 returnval = htp.getName(); 628 } 629 } catch (FinderException e) { 630 } 631 632 debug("<getUserDataSourceName()"); 633 return returnval; 634 } 636 646 private boolean isAuthorizedToUserDataSource(Admin admin, BaseUserDataSource userdatasource) { 647 try { 648 if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){ 649 return true; 650 } 651 652 } catch (AuthorizationDeniedException e) {} 653 try { 654 if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_ADMINISTRATOR)){ 655 if(userdatasource.getApplicableCAs().contains(new Integer (BaseUserDataSource.ANYCA))){ 656 return true; 657 } 658 Collection authorizedcas = getAuthorizationSession().getAuthorizedCAIds(admin); 659 if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){ 660 return true; 661 } 662 } 663 } catch (AuthorizationDeniedException e) {} 664 665 return false; 666 } 667 668 679 private boolean isAuthorizedToEditUserDataSource(Admin admin, BaseUserDataSource userdatasource) { 680 try { 681 if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){ 682 return true; 683 } 684 } catch (AuthorizationDeniedException e) {} 685 try { 686 if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_ADMINISTRATOR) && 687 getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.REGULAR_EDITUSERDATASOURCES)){ 688 if(userdatasource.getApplicableCAs().contains(new Integer (BaseUserDataSource.ANYCA))){ 689 return false; 690 } 691 Collection authorizedcas = getAuthorizationSession().getAuthorizedCAIds(admin); 692 if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){ 693 return true; 694 } 695 } 696 } catch (AuthorizationDeniedException e) {} 697 698 return false; 699 } 700 701 702 private Integer findFreeUserDataSourceId() { 703 Random ran = (new Random ((new Date ()).getTime())); 704 int id = ran.nextInt(); 705 boolean foundfree = false; 706 707 while (!foundfree) { 708 try { 709 if (id > 1) 710 userdatasourcehome.findByPrimaryKey(new Integer (id)); 711 id = ran.nextInt(); 712 } catch (FinderException e) { 713 foundfree = true; 714 } 715 } 716 return new Integer (id); 717 } 719 720 } | Popular Tags |