Get credentials and configs from AuthenticatingHttpServlet, allow main HTTP tunnel...
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicGuacamoleTunnelServlet.java
1 package net.sourceforge.guacamole.net.basic;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Affero General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Affero General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Affero General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 import java.io.IOException;
22 import java.util.Map;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27 import net.sourceforge.guacamole.GuacamoleException;
28 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
29 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
30 import net.sourceforge.guacamole.properties.GuacamoleProperties;
31 import net.sourceforge.guacamole.net.GuacamoleSocket;
32 import net.sourceforge.guacamole.net.GuacamoleTunnel;
33 import net.sourceforge.guacamole.net.auth.Credentials;
34 import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
35 import net.sourceforge.guacamole.servlet.GuacamoleHTTPTunnelServlet;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Connects users to a tunnel associated with the authorized configuration
41  * having the given ID.
42  * 
43  * @author Michael Jumper
44  */
45 public class BasicGuacamoleTunnelServlet extends AuthenticatingHttpServlet {
46
47     private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
48
49     @Override
50     protected void authenticatedService(
51             Map<String, GuacamoleConfiguration> configs,
52             HttpServletRequest request, HttpServletResponse response)
53     throws IOException, ServletException {
54         
55         // If authenticated, respond as tunnel
56         tunnelServlet.service(request, response);
57         
58     }
59
60     /**
61      * Wrapped GuacamoleHTTPTunnelServlet which will handle all authenticated
62      * requests.
63      */
64     private GuacamoleHTTPTunnelServlet tunnelServlet = new GuacamoleHTTPTunnelServlet() {
65
66         @Override
67         protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
68
69             HttpSession httpSession = request.getSession(true);
70
71             // Get ID of connection
72             String id = request.getParameter("id");
73             
74             // Get credentials
75             Credentials credentials = getCredentials(httpSession);
76             
77             // Get authorized configs
78             Map<String, GuacamoleConfiguration> configs = getConfigurations(httpSession);
79
80             // If no configs/credentials in session, not authorized
81             if (credentials == null || configs == null)
82                 throw new GuacamoleException("Cannot connect - user not logged in.");
83
84             // Get authorized config
85             GuacamoleConfiguration config = configs.get(id);
86             if (config == null) {
87                 logger.error("Error retrieving authorized configuration id={}.", id);
88                 throw new GuacamoleException("Unknown configuration ID.");
89             }
90             
91             logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
92
93             // Configure and connect socket
94             String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
95             int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
96
97             GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
98                     new InetGuacamoleSocket(hostname, port),
99                     config
100             );
101
102             // Associate socket with tunnel
103             GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
104
105             return tunnel;
106
107         }
108
109     };
110
111 }
112