1 22 package org.jboss.iiop.csiv2; 23 24 32 33 import java.security.Principal ; 34 35 import org.omg.CORBA.Any ; 36 import org.omg.CORBA.BAD_PARAM ; 37 import org.omg.CORBA.CompletionStatus ; 38 import org.omg.CORBA.LocalObject ; 39 import org.omg.CORBA.MARSHAL ; 40 import org.omg.CORBA.NO_PERMISSION ; 41 import org.omg.CORBA.ORB ; 42 import org.omg.CSI.AuthorizationElement; 43 import org.omg.CSI.EstablishContext; 44 import org.omg.CSI.GSS_NT_ExportedNameHelper; 45 import org.omg.CSI.ITTAnonymous; 46 import org.omg.CSI.IdentityToken; 47 import org.omg.CSI.MTContextError; 48 import org.omg.CSI.SASContextBody; 49 import org.omg.CSI.SASContextBodyHelper; 50 import org.omg.IOP.Codec ; 51 import org.omg.IOP.CodecPackage.FormatMismatch ; 52 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding ; 53 import org.omg.IOP.CodecPackage.TypeMismatch ; 54 import org.omg.IOP.ServiceContext ; 55 import org.omg.IOP.TaggedComponent ; 56 import org.omg.PortableInterceptor.ClientRequestInfo ; 57 import org.omg.PortableInterceptor.ClientRequestInterceptor ; 58 import org.omg.CSIIOP.CompoundSecMech; 59 import org.omg.CSIIOP.CompoundSecMechList; 60 import org.omg.CSIIOP.CompoundSecMechListHelper; 61 import org.omg.CSIIOP.EstablishTrustInClient; 62 import org.omg.CSIIOP.IdentityAssertion; 63 import org.omg.CSIIOP.TAG_CSI_SEC_MECH_LIST; 64 import org.omg.GSSUP.InitialContextToken; 65 import org.jacorb.orb.MinorCodes; 66 import org.jboss.logging.Logger; 67 import org.jboss.security.SecurityAssociation; 68 import org.jboss.security.RunAsIdentity; 69 70 80 public class SASClientIdentityInterceptor 81 extends LocalObject 82 implements ClientRequestInterceptor 83 { 84 private static final Logger log = 85 Logger.getLogger(SASClientIdentityInterceptor.class); 86 private static final boolean traceEnabled = log.isTraceEnabled(); 87 88 89 private static final int sasContextId = 91 org.omg.IOP.SecurityAttributeService.value; 92 93 96 private static final IdentityToken absentIdentityToken; 97 static { 98 absentIdentityToken = new IdentityToken(); 99 absentIdentityToken.absent(true); 100 } 101 private static final AuthorizationElement[] noAuthorizationToken = {}; 102 private static final byte[] noAuthenticationToken = {}; 103 104 private Codec codec; 106 107 111 private static final String serverUsername = "j2ee"; private static final String serverPassword = "j2ee"; 114 116 public SASClientIdentityInterceptor(Codec codec) 117 { 118 this.codec = codec; 119 } 120 121 123 124 126 public String name() 127 { 128 return "SASClientIdentityInterceptor"; 129 } 130 131 public void destroy() 132 { 133 } 135 136 138 public void send_request(ClientRequestInfo ri) 139 { 140 try 141 { 142 CompoundSecMech secMech = 143 CSIv2Util.getMatchingSecurityMech( 144 ri, 145 codec, 146 (short)(EstablishTrustInClient.value 147 + IdentityAssertion.value), 148 (short)0 ); 149 if (secMech == null) 150 return; 151 152 if (traceEnabled) 153 { 154 StringBuffer tmp = new StringBuffer (); 155 CSIv2Util.toString(secMech, tmp); 156 log.trace(tmp); 157 } 158 IdentityToken identityToken = absentIdentityToken; 160 byte[] encodedAuthenticationToken = noAuthenticationToken; 161 162 if ((secMech.sas_context_mech.target_supports 163 & IdentityAssertion.value) != 0) 164 { 165 Principal p = null; 167 RunAsIdentity runAs = SecurityAssociation.peekRunAsIdentity(); 168 if (runAs != null) 169 { 170 p = runAs; 172 } 173 else 174 { 175 p = SecurityAssociation.getPrincipal(); 177 } 178 179 if (p != null) 180 { 181 String name = p.getName(); 183 if (name.indexOf('@') < 0) 184 name += "@default"; byte[] principalName = name.getBytes("UTF-8"); 186 187 byte[] encodedName = 189 CSIv2Util.encodeGssExportedName(principalName); 190 191 Any any = ORB.init().create_any(); 193 byte[] encapsulatedEncodedName = null; 194 GSS_NT_ExportedNameHelper.insert(any, encodedName); 195 try 196 { 197 encapsulatedEncodedName = codec.encode_value(any); 198 } 199 catch (InvalidTypeForEncoding e) 200 { 201 throw new RuntimeException ("Unexpected exception: " + e); 202 } 203 204 identityToken = new IdentityToken(); 206 identityToken.principal_name(encapsulatedEncodedName); 207 } 208 else if ((secMech.sas_context_mech.supported_identity_types 209 & ITTAnonymous.value) != 0) 210 { 211 identityToken = new IdentityToken(); 214 identityToken.anonymous(true); 215 } 216 } 217 218 if ((secMech.as_context_mech.target_requires 219 & EstablishTrustInClient.value) != 0) 220 { 221 byte[] encodedTargetName = secMech.as_context_mech.target_name; 224 String name = serverUsername; 225 if (name.indexOf('@') < 0) 226 { 227 byte[] decodedTargetName = 228 CSIv2Util.decodeGssExportedName(encodedTargetName); 229 String targetName = new String (decodedTargetName, "UTF-8"); 230 name += "@" + targetName; } 232 byte[] username = name.getBytes("UTF-8"); 233 byte[] password = serverPassword.getBytes("UTF-8"); 236 237 InitialContextToken authenticationToken = 239 new InitialContextToken(username, 240 password, 241 encodedTargetName); 242 encodedAuthenticationToken = 244 CSIv2Util.encodeInitialContextToken(authenticationToken, codec); 245 } 246 247 if (identityToken != absentIdentityToken 248 || encodedAuthenticationToken != noAuthenticationToken) 249 { 250 EstablishContext message = 253 new EstablishContext(0, noAuthorizationToken, 255 identityToken, 256 encodedAuthenticationToken); 257 258 SASContextBody contextBody = new SASContextBody(); 260 contextBody.establish_msg(message); 261 262 Any any = ORB.init().create_any(); 264 SASContextBodyHelper.insert(any, contextBody); 265 ServiceContext sc = 266 new ServiceContext (sasContextId, codec.encode_value(any)); 267 ri.add_request_service_context(sc, 268 true ); 269 } 270 } 271 catch (java.io.UnsupportedEncodingException e) 272 { 273 throw new MARSHAL ("Unexpected exception: " + e); 274 } 275 catch (InvalidTypeForEncoding e) 276 { 277 throw new MARSHAL ("Unexpected exception: " + e); 278 } 279 } 280 281 public void send_poll(ClientRequestInfo ri) 282 { 283 } 285 286 public void receive_reply(ClientRequestInfo ri) 287 { 288 try 289 { 290 ServiceContext sc = ri.get_reply_service_context(sasContextId); 291 Any msg = codec.decode_value(sc.context_data, 292 SASContextBodyHelper.type()); 293 SASContextBody contextBody = SASContextBodyHelper.extract(msg); 294 295 300 if (traceEnabled) 301 log.trace("receive_reply: got SAS reply, type " + 302 contextBody.discriminator()); 303 304 if (contextBody.discriminator() == MTContextError.value) 305 { 306 log.warn("Unexpected ContextError in SAS reply"); 308 throw new NO_PERMISSION ("Unexpected ContextError in SAS reply", 309 MinorCodes.SAS_CSS_FAILURE, 310 CompletionStatus.COMPLETED_YES); 311 } 312 } 313 catch (BAD_PARAM e) 314 { 315 } 317 catch (FormatMismatch e) 318 { 319 throw new MARSHAL ("Could not parse SAS reply: " + e, 320 0, 321 CompletionStatus.COMPLETED_YES); 322 } 323 catch (TypeMismatch e) 324 { 325 throw new MARSHAL ("Could not parse SAS reply: " + e, 326 0, 327 CompletionStatus.COMPLETED_YES); 328 } 329 } 330 331 public void receive_exception(ClientRequestInfo ri) 332 { 333 try 334 { 335 ServiceContext sc = ri.get_reply_service_context(sasContextId); 336 Any msg = codec.decode_value(sc.context_data, 337 SASContextBodyHelper.type()); 338 SASContextBody contextBody = SASContextBodyHelper.extract(msg); 339 340 345 if (traceEnabled) 346 log.trace("receive_exceptpion: got SAS reply, type " + 347 contextBody.discriminator()); 348 } 349 catch (BAD_PARAM e) 350 { 351 } 353 catch (FormatMismatch e) 354 { 355 throw new MARSHAL ("Could not parse SAS reply: " + e, 356 MinorCodes.SAS_CSS_FAILURE, 357 CompletionStatus.COMPLETED_MAYBE); 358 } 359 catch (TypeMismatch e) 360 { 361 throw new MARSHAL ("Could not parse SAS reply: " + e, 362 MinorCodes.SAS_CSS_FAILURE, 363 CompletionStatus.COMPLETED_MAYBE); 364 } 365 } 366 367 public void receive_other(ClientRequestInfo ri) 368 { 369 } 371 372 CompoundSecMech getSecurityMech(ClientRequestInfo ri) 373 { 374 CompoundSecMechList csmList = null; 375 CompoundSecMech securityMech = null; 376 try 377 { 378 TaggedComponent tc = ri.get_effective_component(TAG_CSI_SEC_MECH_LIST.value); 379 380 Any any = codec.decode_value(tc.component_data, 381 CompoundSecMechListHelper.type()); 382 383 csmList = CompoundSecMechListHelper.extract(any); 384 385 securityMech = csmList.mechanism_list[0]; 392 } 393 catch (BAD_PARAM e) 394 { 395 } 397 catch (org.omg.IOP.CodecPackage.TypeMismatch tm) 398 { 399 } 401 catch (org.omg.IOP.CodecPackage.FormatMismatch tm) 402 { 403 } 405 return securityMech; 406 } 407 } 408 | Popular Tags |