1 22 package org.jboss.iiop.csiv2; 23 24 32 33 import org.omg.CORBA.Any ; 34 import org.omg.CORBA.BAD_PARAM ; 35 import org.omg.CORBA.MARSHAL ; 36 import org.omg.CORBA.NO_PERMISSION ; 37 import org.omg.CORBA.LocalObject ; 38 import org.omg.CORBA.ORB ; 39 import org.omg.CSI.CompleteEstablishContext; 40 import org.omg.CSI.ContextError; 41 import org.omg.CSI.EstablishContext; 42 import org.omg.CSI.GSS_NT_ExportedNameHelper; 43 import org.omg.CSI.ITTPrincipalName; 44 import org.omg.CSI.IdentityToken; 45 import org.omg.CSI.MTEstablishContext; 46 import org.omg.CSI.MTMessageInContext; 47 import org.omg.CSI.SASContextBody; 48 import org.omg.CSI.SASContextBodyHelper; 49 import org.omg.GSSUP.ErrorToken; 50 import org.omg.GSSUP.ErrorTokenHelper; 51 import org.omg.GSSUP.GSS_UP_S_G_UNSPECIFIED; 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.CodecPackage.InvalidTypeForEncoding ; 57 import org.omg.IOP.ServiceContext ; 58 import org.omg.PortableInterceptor.ServerRequestInfo ; 59 import org.omg.PortableInterceptor.ServerRequestInterceptor ; 60 61 import org.jboss.iiop.CorbaORBService; 62 import org.jboss.logging.Logger; 63 64 73 public class SASTargetInterceptor 74 extends LocalObject 75 implements ServerRequestInterceptor 76 { 77 78 80 private static final Logger log = 81 Logger.getLogger(SASTargetInterceptor.class); 82 private static final boolean traceEnabled = log.isTraceEnabled(); 83 84 private static final int sasContextId = 85 org.omg.IOP.SecurityAttributeService.value; 86 87 private static final byte[] empty = new byte[0]; 88 private static final IdentityToken absent; 89 90 91 private static final SASContextBody msgBodyCtxAccepted; 92 93 95 private static final Any msgCtx0Accepted; 96 97 static 98 { 99 absent = new IdentityToken(); 101 absent.absent(true); 102 103 CompleteEstablishContext ctxAccepted = 108 new CompleteEstablishContext(0, 109 false, 110 new byte[0] ); 111 112 msgBodyCtxAccepted = new SASContextBody(); 113 msgBodyCtxAccepted.complete_msg(ctxAccepted); 114 115 msgCtx0Accepted = createMsgCtxAccepted(0); 117 } 118 119 121 private static Any createMsgCtxAccepted(long contextId) 122 { 123 Any any = ORB.init().create_any(); 124 synchronized (msgBodyCtxAccepted) 125 { 126 msgBodyCtxAccepted.complete_msg().client_context_id = contextId; 127 SASContextBodyHelper.insert(any, msgBodyCtxAccepted); 128 } 129 return any; 130 } 131 132 134 private final Codec codec; 135 136 137 private final SASContextBody msgBodyCtxError; 138 139 141 private final Any msgCtx0Rejected; 142 143 private ThreadLocal threadLocalData = new ThreadLocal () { 144 protected synchronized Object initialValue() 145 { 146 return new CurrentRequestInfo(); } 148 }; 149 150 152 156 private static class CurrentRequestInfo 157 { 158 boolean sasContextReceived; 159 boolean authenticationTokenReceived; 160 byte[] incomingUsername; 161 byte[] incomingPassword; 162 byte[] incomingTargetName; 163 IdentityToken incomingIdentity; 164 byte[] incomingPrincipalName; 165 long contextId; 166 Any sasReply; 167 boolean sasReplyIsAccept; CurrentRequestInfo() 171 { 172 } 173 } 174 175 177 private Any createMsgCtxError(long contextId, int majorStatus) 178 { 179 Any any = ORB.init().create_any(); 180 synchronized (msgBodyCtxError) 181 { 182 msgBodyCtxError.error_msg().client_context_id = contextId; 183 msgBodyCtxError.error_msg().major_status = majorStatus; 184 SASContextBodyHelper.insert(any, msgBodyCtxError); 185 } 186 return any; 187 } 188 189 191 public SASTargetInterceptor(Codec codec) 192 { 193 this.codec = codec; 194 195 ErrorToken errorToken = new ErrorToken(GSS_UP_S_G_UNSPECIFIED.value); 199 Any any = ORB.init().create_any(); 200 byte[] encapsulatedErrorToken; 201 202 ErrorTokenHelper.insert(any, errorToken); 203 try 204 { 205 encapsulatedErrorToken = codec.encode_value(any); 206 } 207 catch (InvalidTypeForEncoding e) 208 { 209 throw new RuntimeException ("Unexpected exception: " + e); 210 } 211 212 ContextError ctxError = 214 new ContextError(0, 215 1, 216 1, 217 encapsulatedErrorToken); 218 219 msgBodyCtxError = new SASContextBody(); 220 msgBodyCtxError.error_msg(ctxError); 221 222 msgCtx0Rejected = createMsgCtxError(0, 1); 224 225 } 226 227 229 232 boolean sasContextReceived() 233 { 234 CurrentRequestInfo threadLocal = 235 (CurrentRequestInfo)threadLocalData.get(); 236 return threadLocal.sasContextReceived; 237 } 238 239 243 boolean authenticationTokenReceived() 244 { 245 CurrentRequestInfo threadLocal = 246 (CurrentRequestInfo)threadLocalData.get(); 247 return threadLocal.authenticationTokenReceived; 248 } 249 250 253 byte[] getIncomingUsername() 254 { 255 CurrentRequestInfo threadLocal = 256 (CurrentRequestInfo)threadLocalData.get(); 257 return threadLocal.incomingUsername; 258 } 259 260 263 byte[] getIncomingPassword() 264 { 265 CurrentRequestInfo threadLocal = 266 (CurrentRequestInfo)threadLocalData.get(); 267 return threadLocal.incomingPassword; 268 } 269 270 273 byte[] getIncomingTargetName() 274 { 275 CurrentRequestInfo threadLocal = 276 (CurrentRequestInfo)threadLocalData.get(); 277 return threadLocal.incomingTargetName; 278 } 279 280 284 IdentityToken getIncomingIdentity() 285 { 286 CurrentRequestInfo threadLocal = 287 (CurrentRequestInfo)threadLocalData.get(); 288 return threadLocal.incomingIdentity; 289 } 290 291 294 byte[] getIncomingPrincipalName() 295 { 296 CurrentRequestInfo threadLocal = 297 (CurrentRequestInfo)threadLocalData.get(); 298 return threadLocal.incomingPrincipalName; 299 } 300 301 305 void rejectIncomingContext() 306 { 307 CurrentRequestInfo threadLocal = 308 (CurrentRequestInfo)threadLocalData.get(); 309 310 if (threadLocal.sasContextReceived) 311 { 312 threadLocal.sasReply = 313 (threadLocal.contextId == 0) 314 ? msgCtx0Rejected 315 : createMsgCtxError(threadLocal.contextId, 316 1 ); 317 threadLocal.sasReplyIsAccept = false; 318 } 319 } 320 321 323 public String name() 324 { 325 return "SASTargetInterceptor"; 326 } 327 328 public void destroy() 329 { 330 } 332 333 335 public void receive_request_service_contexts(ServerRequestInfo ri) 336 { 337 } 339 340 342 public void receive_request(ServerRequestInfo ri) 343 { 344 if (traceEnabled) 345 log.trace("receive_request " + ri.operation()); 346 CurrentRequestInfo threadLocal = 347 (CurrentRequestInfo)threadLocalData.get(); 348 349 threadLocal.sasContextReceived = false; 350 threadLocal.authenticationTokenReceived = false; 351 threadLocal.incomingUsername = empty; 352 threadLocal.incomingPassword = empty; 353 threadLocal.incomingTargetName = empty; 354 threadLocal.incomingIdentity = absent; 355 threadLocal.incomingPrincipalName = empty; 356 threadLocal.sasReply = null; 357 threadLocal.sasReplyIsAccept = false; 358 359 try 360 { 361 ServiceContext sc = ri.get_request_service_context(sasContextId); 362 Any any = codec.decode_value(sc.context_data, 363 SASContextBodyHelper.type()); 364 SASContextBody contextBody = SASContextBodyHelper.extract(any); 365 366 if (contextBody == null) 367 { 368 return; 370 } 371 else if (contextBody.discriminator() == MTMessageInContext.value) 372 { 373 long contextId = 376 contextBody.in_context_msg().client_context_id; 377 threadLocal.sasReply = 378 createMsgCtxError(contextId, 379 4 ); 380 throw new NO_PERMISSION ("SAS context does not exist."); 381 } 382 else if (contextBody.discriminator() == MTEstablishContext.value) 383 { 384 EstablishContext message = contextBody.establish_msg(); 385 threadLocal.contextId = message.client_context_id; 386 threadLocal.sasContextReceived = true; 387 388 if (message.client_authentication_token != null 389 && message.client_authentication_token.length > 0) 390 { 391 if (traceEnabled) 392 log.trace("received client authentication token"); 393 InitialContextToken authToken = 394 CSIv2Util.decodeInitialContextToken( 395 message.client_authentication_token, 396 codec); 397 if (authToken == null) 398 { 399 threadLocal.sasReply = 400 createMsgCtxError(message.client_context_id, 401 2 ); 403 throw new NO_PERMISSION ("Could not decode " + 404 "initial context token."); 405 } 406 threadLocal.incomingUsername = authToken.username; 407 threadLocal.incomingPassword = authToken.password; 408 threadLocal.incomingTargetName = 409 CSIv2Util.decodeGssExportedName(authToken.target_name); 410 if (threadLocal.incomingTargetName == null) 411 { 412 threadLocal.sasReply = 413 createMsgCtxError(message.client_context_id, 414 2 ); 416 throw new NO_PERMISSION ("Could not decode target name " + 417 "in initial context token."); 418 } 419 420 421 threadLocal.authenticationTokenReceived = true; 422 } 423 if (message.identity_token != null) 424 { 425 if (traceEnabled) 426 log.trace("received identity token"); 427 threadLocal.incomingIdentity = message.identity_token; 428 if (message.identity_token.discriminator() == ITTPrincipalName.value) 429 { 430 Any a = codec.decode_value( 433 message.identity_token.principal_name(), 434 GSS_NT_ExportedNameHelper.type()); 435 byte[] encodedName = GSS_NT_ExportedNameHelper.extract(a); 436 437 threadLocal.incomingPrincipalName = 439 CSIv2Util.decodeGssExportedName(encodedName); 440 441 if (threadLocal.incomingPrincipalName == null) 442 { 443 threadLocal.sasReply = 444 createMsgCtxError(message.client_context_id, 445 2 ); 447 throw new NO_PERMISSION ("Could not decode " + 448 "incoming principal name."); 449 } 450 } 451 } 452 threadLocal.sasReply = (threadLocal.contextId == 0) ? 453 msgCtx0Accepted : 454 createMsgCtxAccepted(threadLocal.contextId); 455 threadLocal.sasReplyIsAccept = true; 456 } 457 } 458 catch (BAD_PARAM e) 459 { 460 } 462 catch (FormatMismatch e) 463 { 464 throw new MARSHAL ("Exception decoding context data in " + 465 "SASTargetInterceptor: " + e); 466 } 467 catch (TypeMismatch e) 468 { 469 throw new MARSHAL ("Exception decoding context data in " + 470 "SASTargetInterceptor: " + e); 471 } 472 } 473 474 public void send_reply(ServerRequestInfo ri) 475 { 476 if (traceEnabled) 477 log.trace("send_reply " + ri.operation()); 478 CurrentRequestInfo threadLocal = 479 (CurrentRequestInfo)threadLocalData.get(); 480 481 if (threadLocal.sasReply != null) 482 { 483 try 484 { 485 ServiceContext sc = 486 new ServiceContext (sasContextId, 487 codec.encode_value(threadLocal.sasReply)); 488 ri.add_reply_service_context(sc, true); 489 } 490 catch (InvalidTypeForEncoding e) 491 { 492 throw new MARSHAL ("Unexpected exception: " + e); 493 } 494 } 495 } 496 497 public void send_exception(ServerRequestInfo ri) 498 { 499 if (traceEnabled) 500 log.trace("send_exception " + ri.operation() + ": "); 501 CurrentRequestInfo threadLocal = 502 (CurrentRequestInfo)threadLocalData.get(); 503 504 if (threadLocal.sasReply != null && 515 (!threadLocal.sasReplyIsAccept || 516 CorbaORBService.getSendSASAcceptWithExceptionEnabledFlag() == true)) 517 { 518 try 519 { 520 ServiceContext sc = 521 new ServiceContext (sasContextId, 522 codec.encode_value(threadLocal.sasReply)); 523 ri.add_reply_service_context(sc, true); 524 } 525 catch (InvalidTypeForEncoding e) 526 { 527 throw new MARSHAL ("Unexpected exception: " + e); 528 } 529 } 530 } 531 532 public void send_other(ServerRequestInfo ri) 533 { 534 } 537 } 538 | Popular Tags |