Example #1
0
  /**
   * Stop all forwards giving the resource ID of the <i>SSL-Tunnel</i> that started them.
   *
   * @param launchSession launch session
   * @throws NoPermissionException if not allowed
   * @throws CoreException on any other error
   */
  public void stopTunnels(LaunchSession launchSession) throws NoPermissionException, CoreException {
    if (!DefaultAgentManager.getInstance().hasActiveAgent(launchSession.getSession())) {
      throw new TunnelException(TunnelException.INTERNAL_ERROR, (Throwable) null, "No agent.");
    }

    Tunnel tunnel = (Tunnel) launchSession.getResource();
    launchSession.checkAccessRights(null, agent.getSession());
    MultiplexedConnection agent =
        DefaultAgentManager.getInstance().getAgentBySession(launchSession.getSession());

    try {
      if (tunnel.getType() == TransportType.LOCAL_TUNNEL_ID) {
        Collection<Tunnel> l = new ArrayList<Tunnel>();
        l.add(tunnel);
        stopLocalTunnels(agent, l);
      } else if (tunnel.getType() == TransportType.REMOTE_TUNNEL_ID) {
        Collection<Tunnel> l = new ArrayList<Tunnel>();
        l.add(tunnel);
        stopRemoteTunnels(agent, l);
      } else {
        throw new TunnelException(
            TunnelException.INTERNAL_ERROR,
            (Throwable) null,
            "Unknown tunnel type " + tunnel.getType());
      }

      CoreServlet.getServlet()
          .fireCoreEvent(
              new ResourceAccessEvent(
                  this,
                  TunnelsEventConstants.TUNNEL_CLOSED,
                  launchSession.getResource(),
                  launchSession.getPolicy(),
                  launchSession.getSession(),
                  CoreEvent.STATE_SUCCESSFUL));

    } catch (TunnelException te) {
      CoreServlet.getServlet()
          .fireCoreEvent(
              new ResourceAccessEvent(
                  this,
                  TunnelsEventConstants.TUNNEL_CLOSED,
                  launchSession.getResource(),
                  launchSession.getPolicy(),
                  launchSession.getSession(),
                  te));
      throw te;
    } finally {
      LaunchSessionFactory.getInstance().removeLaunchSession(launchSession);
    }
  }
Example #2
0
  /**
   * Start port forwards for the <i>SSL Tunnel</i> specified by the provided resource ID.
   *
   * @param launchSession launch session
   * @throws NoPermissionException if not allowed
   * @throws TunnelException on any other other
   * @throws PolicyException on any other determininig policy
   */
  public void startTunnel(LaunchSession launchSession)
      throws NoPermissionException, TunnelException, PolicyException {

    if (!DefaultAgentManager.getInstance().hasActiveAgent(launchSession.getSession())) {
      throw new TunnelException(TunnelException.INTERNAL_ERROR, (Throwable) null, "No agent.");
    } else {
      Tunnel tunnel = (Tunnel) launchSession.getResource();
      launchSession.checkAccessRights(null, agent.getSession());
      AgentTunnel agent =
          DefaultAgentManager.getInstance().getAgentBySession(launchSession.getSession());

      try {
        if (tunnel.getType() == TransportType.LOCAL_TUNNEL_ID) {
          startLocalTunnel(agent, tunnel, launchSession);
        } else if (tunnel.getType() == TransportType.REMOTE_TUNNEL_ID) {
          startRemoteTunnel(agent, tunnel, launchSession);
        } else {
          throw new TunnelException(
              TunnelException.INTERNAL_ERROR,
              (Throwable) null,
              "Unknown tunnel type " + tunnel.getType());
        }

        // Fire event
        CoreServlet.getServlet()
            .fireCoreEvent(
                new ResourceAccessEvent(
                    this,
                    TunnelsEventConstants.TUNNEL_OPENED,
                    launchSession.getResource(),
                    launchSession.getPolicy(),
                    launchSession.getSession(),
                    CoreEvent.STATE_SUCCESSFUL));
      } catch (TunnelException te) {

        // Fire event
        CoreServlet.getServlet()
            .fireCoreEvent(
                new ResourceAccessEvent(
                    this,
                    TunnelsEventConstants.TUNNEL_OPENED,
                    launchSession.getResource(),
                    launchSession.getPolicy(),
                    launchSession.getSession(),
                    te));

        throw te;
      }
    }
  }
Example #3
0
 public boolean processRequest(Request request, MultiplexedConnection connection) {
   AgentTunnel agent = (AgentTunnel) connection;
   if (request.getRequestName().equals(SETUP_AND_LAUNCH_TUNNEL)
       && request.getRequestData() != null) {
     try {
       ByteArrayReader reader = new ByteArrayReader(request.getRequestData());
       int id = (int) reader.readInt();
       Tunnel resource = (Tunnel) TunnelPlugin.SSL_TUNNEL_RESOURCE_TYPE.getResourceById(id);
       if (resource == null) {
         throw new Exception("No resource with ID " + id);
       }
       Policy policy =
           LaunchSessionManager.getLaunchRequestPolicy(null, agent.getSession(), resource);
       if (resource.sessionPasswordRequired(agent.getSession())) {
         // TODO: prompt user for credentials through agent!
         return true;
       } else {
         LaunchSession launchSession =
             LaunchSessionFactory.getInstance()
                 .createLaunchSession(agent.getSession(), resource, policy);
         launchSession.checkAccessRights(null, agent.getSession());
         if (resource.getType() == TransportType.LOCAL_TUNNEL_ID) {
           try {
             Request req = buildLocalTunnel(resource, launchSession);
             request.setRequestData(req.getRequestData());
             return true;
           } catch (IOException ioe) {
             throw new TunnelException(TunnelException.INTERNAL_ERROR, ioe);
           }
         } else if (resource.getType() == TransportType.REMOTE_TUNNEL_ID) {
           startRemoteTunnel(agent, resource, launchSession);
           request.setRequestData(null);
           return true;
         } else {
           throw new TunnelException(
               TunnelException.INTERNAL_ERROR,
               (Throwable) null,
               "Unknown tunnel type " + resource.getType());
         }
       }
     } catch (Exception e) {
       log.error("Failed to start tunnel.", e);
       return false;
     }
   }
   return false;
 }
Example #4
0
  Request buildLocalTunnel(Tunnel tunnel, LaunchSession launchSession) throws IOException {
    // Process destination host and port for replacement variables
    VariableReplacement r = new VariableReplacement();
    r.setLaunchSession(launchSession);
    String destHost = r.replace(tunnel.getDestination().getHost());

    ByteArrayWriter msg = new ByteArrayWriter();
    msg.writeString(launchSession == null ? "" : launchSession.getId());
    msg.writeInt(tunnel.getResourceId());
    msg.writeString(tunnel.getResourceName());
    msg.writeInt(tunnel.getType());
    msg.writeString(tunnel.getTransport());
    msg.writeString(tunnel.getSourceInterface());
    msg.writeInt(tunnel.getSourcePort());
    msg.writeInt(tunnel.getDestination().getPort());
    msg.writeString(destHost);
    Request req = new Request(START_LOCAL_TUNNEL, msg.toByteArray());
    return req;
  }