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.MARSHAL ; 38 import org.omg.CORBA.NO_PERMISSION ; 39 import org.omg.CORBA.ORB ; 40 import org.omg.CORBA.CompletionStatus ; 41 import org.omg.CORBA.LocalObject ; 42 import org.omg.CSI.AuthorizationElement; 43 import org.omg.CSI.EstablishContext; 44 import org.omg.CSI.IdentityToken; 45 import org.omg.CSI.MTContextError; 46 import org.omg.CSI.SASContextBody; 47 import org.omg.CSI.SASContextBodyHelper; 48 49 import org.omg.CSIIOP.CompoundSecMech; 50 import org.omg.CSIIOP.EstablishTrustInClient; 51 52 import org.omg.GSSUP.InitialContextToken; 53 import org.omg.IOP.Codec ; 54 import org.omg.IOP.CodecPackage.FormatMismatch ; 55 import org.omg.IOP.CodecPackage.TypeMismatch ; 56 import org.omg.IOP.ServiceContext ; 57 import org.omg.PortableInterceptor.ClientRequestInfo ; 58 import org.omg.PortableInterceptor.ClientRequestInterceptor ; 59 import org.jacorb.orb.MinorCodes; 60 61 import org.jboss.logging.Logger; 62 import org.jboss.security.SecurityAssociation; 63 64 74 public class SASClientInterceptor 75 extends LocalObject 76 implements ClientRequestInterceptor 77 { 78 private static final int sasContextId = 80 org.omg.IOP.SecurityAttributeService.value; 81 82 private static final IdentityToken absentIdentityToken; 83 static { 84 absentIdentityToken = new IdentityToken(); 85 absentIdentityToken.absent(true); 86 } 87 private static final AuthorizationElement[] noAuthorizationToken = {}; 88 89 private static final Logger log = 90 Logger.getLogger(SASTargetInterceptor.class); 91 private static final boolean traceEnabled = log.isTraceEnabled(); 92 93 94 96 private Codec codec; 97 98 100 public SASClientInterceptor(Codec codec) 101 { 102 this.codec = codec; 103 } 104 105 107 108 110 public String name() 111 { 112 return "SASClientInterceptor"; 113 } 114 115 public void destroy() 116 { 117 } 119 120 122 public void send_request(ClientRequestInfo ri) 123 { 124 try 125 { 126 CompoundSecMech secMech = 127 CSIv2Util.getMatchingSecurityMech( 128 ri, 129 codec, 130 EstablishTrustInClient.value, 131 (short)0 ); 132 if (secMech == null) 133 return; 134 135 if ((secMech.as_context_mech.target_supports 136 & EstablishTrustInClient.value) != 0) 137 { 138 Principal p = SecurityAssociation.getPrincipal(); 139 if (p != null) 140 { 141 byte[] encodedTargetName = secMech.as_context_mech.target_name; 142 143 String name = p.getName(); 145 if (name.indexOf('@') < 0) 146 { 147 byte[] decodedTargetName = 148 CSIv2Util.decodeGssExportedName(encodedTargetName); 149 String targetName = new String (decodedTargetName, "UTF-8"); 150 name += "@" + targetName; } 152 byte[] username = name.getBytes("UTF-8"); 153 Object credential = SecurityAssociation.getCredential(); 156 byte[] password = {}; 157 if (credential instanceof char[]) 158 { 159 String tmp = new String ((char[]) credential); 160 password = tmp.getBytes("UTF-8"); 161 } 162 else if (credential instanceof byte[]) 163 password = (byte[])credential; 164 else if (credential != null) 165 { 166 String tmp = credential.toString(); 167 password = tmp.getBytes("UTF-8"); 168 } 169 170 InitialContextToken authenticationToken = 172 new InitialContextToken(username, 173 password, 174 encodedTargetName); 175 byte[] encodedAuthenticationToken = 177 CSIv2Util.encodeInitialContextToken(authenticationToken, 178 codec); 179 180 EstablishContext message = 182 new EstablishContext(0, noAuthorizationToken, 184 absentIdentityToken, 185 encodedAuthenticationToken); 186 187 SASContextBody contextBody = new SASContextBody(); 189 contextBody.establish_msg(message); 190 191 Any any = ORB.init().create_any(); 193 SASContextBodyHelper.insert(any, contextBody); 194 ServiceContext sc = 195 new ServiceContext (sasContextId, codec.encode_value(any)); 196 ri.add_request_service_context(sc, 197 true ); 198 } 199 } 200 } 201 catch (java.io.UnsupportedEncodingException e) 202 { 203 throw new MARSHAL ("Unexpected exception: " + e); 204 } 205 catch (org.omg.IOP.CodecPackage.InvalidTypeForEncoding e) 206 { 207 throw new MARSHAL ("Unexpected exception: " + e); 208 } 209 } 210 211 public void send_poll(ClientRequestInfo ri) 212 { 213 } 215 216 public void receive_reply(ClientRequestInfo ri) 217 { 218 try 219 { 220 ServiceContext sc = ri.get_reply_service_context(sasContextId); 221 Any msg = codec.decode_value(sc.context_data, 222 SASContextBodyHelper.type()); 223 SASContextBody contextBody = SASContextBodyHelper.extract(msg); 224 225 230 if (traceEnabled) 231 log.trace("receive_reply: got SAS reply, type " + 232 contextBody.discriminator()); 233 234 if (contextBody.discriminator() == MTContextError.value) 235 { 236 log.warn("Unexpected ContextError in SAS reply"); 238 throw new NO_PERMISSION ("Unexpected ContextError in SAS reply", 239 MinorCodes.SAS_CSS_FAILURE, 240 CompletionStatus.COMPLETED_YES); 241 } 242 } 243 catch (BAD_PARAM e) 244 { 245 } 247 catch (FormatMismatch e) 248 { 249 throw new MARSHAL ("Could not parse SAS reply: " + e, 250 0, 251 CompletionStatus.COMPLETED_YES); 252 } 253 catch (TypeMismatch e) 254 { 255 throw new MARSHAL ("Could not parse SAS reply: " + e, 256 0, 257 CompletionStatus.COMPLETED_YES); 258 } 259 } 260 261 public void receive_exception(ClientRequestInfo ri) 262 { 263 try 264 { 265 ServiceContext sc = ri.get_reply_service_context(sasContextId); 266 Any msg = codec.decode_value(sc.context_data, 267 SASContextBodyHelper.type()); 268 SASContextBody contextBody = SASContextBodyHelper.extract(msg); 269 270 275 if (traceEnabled) 276 log.trace("receive_exception: got SAS reply, type " + 277 contextBody.discriminator()); 278 } 279 catch (BAD_PARAM e) 280 { 281 } 283 catch (FormatMismatch e) 284 { 285 throw new MARSHAL ("Could not parse SAS reply: " + e, 286 MinorCodes.SAS_CSS_FAILURE, 287 CompletionStatus.COMPLETED_MAYBE); 288 } 289 catch (TypeMismatch e) 290 { 291 throw new MARSHAL ("Could not parse SAS reply: " + e, 292 MinorCodes.SAS_CSS_FAILURE, 293 CompletionStatus.COMPLETED_MAYBE); 294 } 295 } 296 297 public void receive_other(ClientRequestInfo ri) 298 { 299 } 301 302 } 303 | Popular Tags |