summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/SSLIOP/SSLIOP_Invocation_Interceptor.cpp
blob: 57bedb1ed32c2883771ef27500e4175132a7c4b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "orbsvcs/SSLIOP/SSLIOP_Invocation_Interceptor.h"
#include "orbsvcs/SSLIOP/SSLIOP_Current.h"

#include "orbsvcs/SecurityLevel2C.h"

#include "tao/ORB_Constants.h"
#include "tao/PortableServer/PS_CurrentC.h"
#include "tao/PortableServer/POAC.h"
#include "tao/debug.h"

#if defined(SSLIOP_DEBUG_PEER_CERTIFICATE)
#include <openssl/x509.h>   // @@ For debugging code below
#endif /* DEBUG_PEER_CERTIFICATES */

ACE_RCSID (SSLIOP,
           SSLIOP_Invocation_Interceptor,
           "$Id$")


TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO::SSLIOP::Server_Invocation_Interceptor::Server_Invocation_Interceptor
(
  PortableInterceptor::ORBInitInfo_ptr info,
  ::Security::QOP default_qop,
  size_t tss_slot
)
: qop_ (default_qop)
{
  /*
   * Cache references to the "Current" objects that we'll need during
   * during invocations.
   */

  CORBA::Object_var obj =
    info->resolve_initial_references ("SSLIOPCurrent");

  this->ssliop_current_ = ::SSLIOP::Current::_narrow (obj.in ());

  if (!CORBA::is_nil (this->ssliop_current_.in ()))
    {
      TAO::SSLIOP::Current *tao_current =
        dynamic_cast<TAO::SSLIOP::Current *> (this->ssliop_current_.in ());

      if (tao_current != 0)
        {
          if (TAO_debug_level > 3)
            ACE_DEBUG ((LM_DEBUG, "TAO (%P|%t) SSLIOP_Invocation_Interceptor::CTOR--setting up SSLIOP Current with slot %d\n", tss_slot));
          tao_current->tss_slot (tss_slot);
        }
      else
        throw CORBA::INTERNAL ();
    }

  obj = info->resolve_initial_references ("SecurityLevel2:SecurityManager");
  this->sec2manager_ = SecurityLevel2::SecurityManager::_narrow (obj.in ());

  if (! CORBA::is_nil (this->sec2manager_.in ()))
    {
      // set the slot id?
    }

  obj = info->resolve_initial_references ("POACurrent");
  this->poa_current_ = PortableServer::Current::_narrow (obj.in ());
}

TAO::SSLIOP::Server_Invocation_Interceptor::~Server_Invocation_Interceptor (
  void)
{
}


char *
TAO::SSLIOP::Server_Invocation_Interceptor::name ()
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  return CORBA::string_dup ("TAO::SSLIOP::Server_Invocation_Interceptor");
}

void
TAO::SSLIOP::Server_Invocation_Interceptor::destroy ()
  ACE_THROW_SPEC ((CORBA::SystemException))
{
}


void
TAO::SSLIOP::Server_Invocation_Interceptor::receive_request_service_contexts (
	PortableInterceptor::ServerRequestInfo_ptr /* ri */)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   PortableInterceptor::ForwardRequest))
{
#if 1
  // This operation is where the old security implementation used to
  // makes its simplistic decision whether to permit or deny, but with
  // the introduction of more detailed checking, there's a need to
  // have access to information that's not available until later in
  // the PI flow.  So, it's now all moved into receive_request().


      CORBA::Boolean const no_ssl =
	this->ssliop_current_->no_context ();

      if (TAO_debug_level >= 3)
	ACE_DEBUG ((LM_DEBUG, "SSLIOP (%P|%t) Interceptor (context), ssl=%d\n", !(no_ssl)));

      // if
      // (1) no SSL session state is available (which means that the
      //     invocation is received across a non-SSL transport)
      // AND
      // (2) the required Quality of Protection is something other
      //     than SecQOPNoProtection (set via -SSLNoProtection)
      if (no_ssl && this->qop_ != ::Security::SecQOPNoProtection)
	throw CORBA::NO_PERMISSION ();
#endif
}


void
TAO::SSLIOP::Server_Invocation_Interceptor::receive_request (
    PortableInterceptor::ServerRequestInfo_ptr ri)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   PortableInterceptor::ForwardRequest))
{
#if 0
  // The current upcall is not being performed through an SSL
  // connection.  If server is configured to disallow insecure
  // invocations then throw a CORBA::NO_PERMISSION exception.
  // @@ TODO: Once the SecurityManager is implemented, query it
  //          for the current object's
  //          SecureInvocationPolicy of type
  //          SecTargetSecureInvocationPolicy so that we can
  //          accept or reject requests on a per-object basis
  //          instead on a per-endpoint basis.

#if 1
  /*
  // Of course, since we're implementing a SecurityManager, we can do
  // this now!  But we want to keep the default behavior, too...

  Q: It's also not clear how we're supposed to query the securitymanager
  "for the current object's SecureInvocationPolicy".  Maybe we need to
  query the policies on the object itself?  Maybe we can ask the
  ServerRequestInfo via get_server_policy()?

  A: The spec says that "This operation returns the policy in effect
  for this operation for the given policy type.  The returned
  CORBA::Policy object shall only be a policy whose type was
  registered via register_policy_factory()", so that should give us
  what we want.
  */
  
  SecurityLevel2::AccessDecision_var ad =
    this->sec2manager_->access_decision ();

  
      CORBA::Boolean const no_ssl =
	this->ssliop_current_->no_context ();

      if (TAO_debug_level >= 3)
	ACE_DEBUG ((LM_DEBUG, "SSLIOP (%P|%t) Interceptor (context), ssl=%d\n", !(no_ssl)));

      // if
      // (1) no SSL session state is available (which means that the
      //     invocation is received across a non-SSL transport)
      // AND
      // (2) the required Quality of Protection is something other
      //     than SecQOPNoProtection (set via -SSLNoProtection)
      if (no_ssl && this->qop_ != ::Security::SecQOPNoProtection)
#if 0
	throw CORBA::NO_PERMISSION ();
  if (   ! CORBA:is_nil (ad.in ())
      && ri->get_server_policy(SecureInvocationPolicy) == SecTargetSecureInvocationPolicy)
#endif
    {
      /*
       * Set up all the arguments needed by the call
       * to AccessDecision::access_allowed()
       */
      
      /* Get the credentials from SSLIOP */
      SecurityLevel2::CredentialsList cred_list; // initial empty?
#if 0
      try {
        SecurityLevel2::ReceivedCredentials_var rcvd_creds =
          this->sec2_current_->received_credentials ();
        // this gets the credentials received from the other side.  We
        // should be able to put this into a CredentialsList with no
        // problem.
        //
        // Do I really need to implement a sec2_current, or can I hack
        // the conversion at this level?  I probably ought to do it as
        // a real sec2_current with the conversion from sec3->sec2
        // happening at a lower level.
        
	cred_list.length(1);
	cred_list[0] = rcvd_creds.in ();
	/*
	  So, in looking for how we can do this, I find that the
	  SL3_SecurityCurrent::client_credentials() delegates to SL3_SecurityCurrent_Impl::client_credentials(), which is pure virtual.
	*/
      }
      catch (...) {
      }
#endif

      /* Get the target object */
      CORBA::Object_var target = CORBA::Object::_nil ();

#if 0 // we want to use the POACurrent::get_reference()!
      // I suppose that we can derive this using server_id, adapter_id
      // and object_id using id_to_reference.  One problem is that,
      // according to the spec, id_to_reference() can only be used
      // when the POA in question has the RETAIN policy so this would
      // be unavailable when the POA has NO_RETAIN.
      try {
	int no_args = 0;
	CORBA::ORB_var orb = CORBA::ORB_init (no_args, 0, ri->orb_id());
	CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
	PortableServer::POA_var rootpoa = 
	  PortableServer::POA::_narrow (obj.in ());
	if (CORBA::is_nil (rootpoa.in ()))
	  // Probably not a good choice for exception, but we need to
	  // throw something.
	  throw PortableServer::POA::AdapterNonExistent();
	
	// @@CJC Grrr...of course nothing is ever easy.  What we'd really
	// like to be able to do is something like...
	//    rootpoa->find_POA (ri->adapter_id (), false); // don't activate
	// to get a reference to the POA indicated here.  Sigh.  CORBA 3.0
	// provides adapter_name(), so maybe I'll get lucky and that'll be
	// available.  Otherwise, I'm going to have to resort to ugliness
	// and proprietary hacks to get this info, b/c adapter_id()
	// returns an opaque octet sequence.
	//
	// The good news is that adapter_name() exists, but it doesn't
	// return a name suitable for submission to find_POA().  Rather,
	// it returns a string sequence.  Exactly what's in that sequence
	// we don't know, but I'll guess that it's supposed to be an in-order
	// list of the poa path down to the poa that's hosting the target
	// CORBA Object.  So, if we look at the last element in the sequence
	// we should get the name of the hosting POA.  <fingers crossed>
	PortableInterceptor::AdapterName_var poa_path = ri->adapter_name ();
	size_t last_component_index = poa_path->length() - 1;
	
	const char* last_component = poa_path[last_component_index];
	PortableServer::POA_var poa =
	  rootpoa->find_POA (last_component,
			     false); // don't activate
     
	// Yow.  Sometimes the C++ mapping mystifies me in its oddities.
	// One would expect that, since the requestinfo has an ObjectId
	// attribute, and the id_to_reference() operation takes an ObjectId
	// as an "in" argument, that one could simply do
	//   target = poa->id_to_reference (ri->object_id())
	// Alas, 'tis not that simple.  The mapping for a return value for
	// an unbounded sequence is T*, and the mapping for an "in" is
	// const T&, which means that we have to dereference the pointer.
	//
	// Damn, that's really ugly.  But I'm glad that's off my chest.
	target = poa->id_to_reference (*(ri->object_id ()));
      }
#define quote(x) #x
      catch (const PortableServer::POA::ObjectNotActive& e) {
	// thrown by id_to_reference()
	e._tao_print_exception ("ssliop_invocation_interceptor::receive_request @ " quote(__LINE__));
      }
      catch (const PortableServer::POA::WrongPolicy& e) {
	// thrown by id_to_reference()
	e._tao_print_exception ("ssliop_invocation_interceptor::receive_request @ " quote(__LINE__));
      }
      catch (const PortableServer::POA::AdapterNonExistent& e) {
	// thrown by find_POA()
	e._tao_print_exception ("yoyoyo ssliop_invocation_interceptor::receive_request @ " quote(__LINE__));
      }
      catch (const CORBA::ORB::InvalidName& e) {
	// thrown by resolve_initial_references()
	e._tao_print_exception ("ssliop_invocation_interceptor::receive_request @ " quote(__LINE__));
      }
#else
      target = this->poa_current_->get_reference ();
#endif

      char* operation_name = ri->operation ();
      char* target_interface_name = ri->target_most_derived_interface(); // is this the repository ID?

      CORBA::Boolean it_should_happen = false;
      if (ad)
	it_should_happen = ad->access_allowed (cred_list,
							    target.in(),
							    operation_name,
							    target_interface_name);
      else
	it_should_happen = true;  // if no access_decision, then let it all through

      if (! it_should_happen)
	{
	  throw CORBA::NO_PERMISSION ();
	}
    }
  else
#endif
    {
      // PREVIOUS DEFAULT BEHAVIOR

      CORBA::Boolean const no_ssl =
	this->ssliop_current_->no_context ();

      if (TAO_debug_level >= 3)
	ACE_DEBUG ((LM_DEBUG, "SSLIOP (%P|%t) Interceptor (context), ssl=%d\n", !(no_ssl)));

      // if
      // (1) no SSL session state is available (which means that the
      //     invocation is received across a non-SSL transport)
      // AND
      // (2) the required Quality of Protection is something other
      //     than SecQOPNoProtection (set via -SSLNoProtection)
      if (no_ssl && this->qop_ != ::Security::SecQOPNoProtection)
	throw CORBA::NO_PERMISSION ();
    }

#if defined(DEBUG_PEER_CERTIFICATES)
  try
    {
      // If the request was not made through an SSL connection, then
      // this method will throw the SSLIOP::Current::NoContext
      // exception.  Otherwise, it will return a DER encoded X509
      // certificate.
      ::SSLIOP::ASN_1_Cert_var cert =
        this->ssliop_current_->get_peer_certificate ();

      // @@ The following debugging code works but I don't think that
      //    we should include it since it dumps alot of information,
      //    i.e. prints two lines of information per request.
      if (TAO_debug_level > 1)
        {
          CORBA::Octet *der_cert = cert->get_buffer ();

          X509 *peer = ::d2i_X509 (0, &der_cert, cert->length ());
          if (peer != 0)
            {
              char buf[BUFSIZ] = { 0 };

              ::X509_NAME_oneline (::X509_get_subject_name (peer),
                                   buf,
                                   BUFSIZ);

              ACE_DEBUG ((LM_DEBUG,
                          "(%P|%t) Certificate subject: %s\n",
                          buf));

              ::X509_NAME_oneline (::X509_get_issuer_name (peer),
                                   buf,
                                   BUFSIZ);

              ACE_DEBUG ((LM_DEBUG,
                          "(%P|%t) Certificate issuer: %s\n",
                          buf));


              ::X509_free (peer);
            }
        }
    }
  catch (const ::SSLIOP::Current::NoContext& )
    {
      // The current upcall is not being performed through an SSL
      // connection.  If server is configured to disallow insecure
      // invocations then throw a CORBA::NO_PERMISSION exception.
      // @@ TODO: Once the SecurityManager is implemented, query it
      //          for the current object's
      //          SecureInvocationPolicy of type
      //          SecTargetSecureInvocationPolicy so that we can
      //          accept or reject requests on a per-object basis
      //          instead on a per-endpoint basis.
      if (this->qop_ != ::Security::SecQOPNoProtection)
        throw CORBA::NO_PERMISSION ();
    }
#endif /* DEBUG_PEER_CERTIFICATES */
#endif
}

void
TAO::SSLIOP::Server_Invocation_Interceptor::send_reply (
    PortableInterceptor::ServerRequestInfo_ptr /* ri */)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
}

void
TAO::SSLIOP::Server_Invocation_Interceptor::send_exception (
    PortableInterceptor::ServerRequestInfo_ptr /* ri */)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   PortableInterceptor::ForwardRequest))
{
}

void
TAO::SSLIOP::Server_Invocation_Interceptor::send_other (
    PortableInterceptor::ServerRequestInfo_ptr /* ri */)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   PortableInterceptor::ForwardRequest))
{
}

TAO_END_VERSIONED_NAMESPACE_DECL