1 23 24 package com.sun.enterprise.iiop.security; 25 26 import java.net.Socket ; 27 import java.security.Principal ; 28 import java.security.ProtectionDomain ; 29 import java.security.CodeSource ; 30 import java.security.Policy ; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import java.security.cert.X509Certificate ; 34 import javax.security.auth.Subject ; 35 import com.sun.enterprise.log.Log; 36 import com.sun.enterprise.util.ORBManager; 37 import com.sun.enterprise.InvocationManager; 38 import com.sun.enterprise.ComponentInvocation; 39 import com.sun.enterprise.InvocationException; 40 import com.sun.enterprise.Switch; 41 import com.sun.enterprise.security.LoginContext; 42 import com.sun.enterprise.security.CORBAObjectPermission; 43 import com.sun.enterprise.security.auth.login.PasswordCredential; 44 import com.sun.enterprise.security.auth.LoginContextDriver; 45 import com.sun.enterprise.iiop.POAProtocolMgr; 46 import com.sun.corba.ee.spi.ior.IOR; 47 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter; 48 49 import java.util.Set ; 50 import java.util.logging.*; 51 import com.sun.logging.*; 52 53 59 60 public class SecurityServiceImpl implements SecurityService 61 { 62 63 private static java.util.logging.Logger _logger=null; 64 static{ 65 _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER); 66 } 67 private static String IS_A = "_is_a"; 69 70 private Policy policy; 71 72 73 public SecurityServiceImpl() { 74 75 policy = Policy.getPolicy(); 76 } 77 78 79 87 public SecurityContext getSecurityContext( 88 org.omg.CORBA.Object effective_target) 89 throws InvalidMechanismException, InvalidIdentityTokenException 90 { 91 SecurityContext context = null; 92 93 IOR ior = ((com.sun.corba.ee.spi.orb.ORB)ORBManager.getORB()).getIOR(effective_target, false); 94 if (StubAdapter.isStub(effective_target)) { 95 if (StubAdapter.isLocal(effective_target)) { 96 return null; 97 } 98 } 99 100 try { 101 SecurityMechanismSelector sms = new SecurityMechanismSelector(); 102 context = sms.selectSecurityContext(ior); 103 } catch (InvalidMechanismException ime){ _logger.log(Level.SEVERE,"iiop.invalidmechanism_exception",ime); 105 throw new InvalidMechanismException (ime.getMessage()); 106 } catch(InvalidIdentityTokenException iite){ 107 _logger.log(Level.SEVERE,"iiop.invalididtoken_exception",iite); 108 throw new InvalidIdentityTokenException(iite.getMessage()); 109 } catch(SecurityMechanismException sme) { 111 _logger.log(Level.SEVERE,"iiop.secmechanism_exception",sme); 112 throw new RuntimeException (sme.getMessage()); 113 } 114 return context; 115 } 116 117 130 public void receivedReply(int reply_status, 131 org.omg.CORBA.Object effective_target) 132 { 133 if(reply_status == STATUS_FAILED) { 134 if(_logger.isLoggable(Level.FINE)){ 135 _logger.log(Level.FINE,"Failed status"); 136 } 137 throw new RuntimeException ("Target did not accept security context"); 139 } else if(reply_status == STATUS_RETRY) { 140 if(_logger.isLoggable(Level.FINE)){ 141 _logger.log(Level.FINE,"Retry status"); 142 } 143 } else { 144 if(_logger.isLoggable(Level.FINE)){ 145 _logger.log(Level.FINE,"Passed status"); 146 } 147 } 148 } 149 150 159 public int setSecurityContext(SecurityContext context, byte[] object_id, 160 String method) 161 { 162 if(_logger.isLoggable(Level.FINE)){ 163 _logger.log(Level.FINE,"ABOUT TO EVALUATE TRUST"); 164 } 165 166 try { 167 SecurityMechanismSelector sms = new SecurityMechanismSelector(); 172 SecurityContext ssc = sms.evaluateTrust(context, object_id); 173 174 Class cls = null; 175 Subject s = null; 176 if(ssc == null) { 177 return STATUS_PASSED; 178 } else { 179 if(ssc.authcls != null) { 180 cls = ssc.authcls; 181 } else { 182 cls = ssc.identcls; 183 } 184 s = ssc.subject; 185 } 186 187 authenticate(s, cls); 190 191 if ( authorizeCORBA(object_id, method) ) 194 return STATUS_PASSED; 195 else 196 return STATUS_FAILED; 197 198 } catch(Exception e) { 199 if (!method.equals(IS_A)){ 200 if(_logger.isLoggable(Level.FINE)){ 201 _logger.log(Level.FINE,"iiop.authenticate_exception",e.toString()); 202 } 203 if(_logger.isLoggable(Level.FINE)){ 204 _logger.log(Level.FINE,"Authentication Exception",e); 205 } 206 } 207 return STATUS_FAILED; 208 } 209 } 210 211 212 private boolean authorizeCORBA(byte[] object_id, String method) 214 throws Exception { 215 216 POAProtocolMgr protocolMgr = (POAProtocolMgr) 218 Switch.getSwitch().getProtocolManager(); 219 if ( protocolMgr == null ) 223 return true; 224 225 if ( protocolMgr.getEjbDescriptor(object_id) != null ) 226 return true; 228 CORBAObjectPermission perm = new CORBAObjectPermission("*", method); 229 230 com.sun.enterprise.security.SecurityContext sc = 232 com.sun.enterprise.security.SecurityContext.getCurrent(); 233 Set principalSet = sc.getPrincipalSet(); 234 Principal [] principals = (principalSet == null ? null : 235 (Principal []) principalSet.toArray(new Principal [0])); 236 CodeSource cs = new CodeSource (new java.net.URL ("file://"), 237 (java.security.cert.Certificate []) null); 238 ProtectionDomain prdm = new ProtectionDomain (cs, null, null, principals); 239 240 boolean result = policy.implies(prdm, perm); 242 243 if ( _logger.isLoggable(Level.FINE) ) { 244 _logger.log(Level.FINE, "CORBA Object permission evaluation result=" 245 + result + " for method=" + method); 246 } 247 return result; 248 } 249 250 251 256 public void sendingReply(SecurityContext context) 257 { 258 } 260 265 public void unsetSecurityContext(){ 266 269 boolean isLocal = true; 271 ServerConnectionContext scc = 272 SecurityMechanismSelector.getServerConnectionContext(); 273 if ( scc != null && scc.getSocket() != null ) 274 isLocal = false; 275 276 if ( !isLocal) 277 com.sun.enterprise.security.SecurityContext.setCurrent(null); 278 } 279 280 284 private void authenticate(Subject s, Class cls) 285 throws SecurityMechanismException 286 { 287 try { 289 final Subject fs = s; 290 final Class cl = cls; 291 AccessController.doPrivileged(new PrivilegedAction () { 292 public java.lang.Object run() { 293 LoginContextDriver.login(fs, cl); 294 return null; 295 } 296 }); 297 } catch(Exception e) { 298 if(_logger.isLoggable(Level.SEVERE)){ 299 _logger.log(Level.SEVERE,"iiop.login_exception",e.toString()); 300 } 301 if(_logger.isLoggable(Level.FINE)){ 302 _logger.log(Level.FINE,"Login Exception",e); 303 } 304 throw new SecurityMechanismException("Cannot login user:" + 305 e.getMessage()); 306 } 307 } 308 309 } 310 311 312 | Popular Tags |