59c096947cda900dcf711a24b3f275c0a4ed5c39
[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 net.sourceforge.guacamole.net.auth.AuthenticationProvider;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpSession;
25 import net.sourceforge.guacamole.GuacamoleException;
26 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
27 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
28 import net.sourceforge.guacamole.properties.GuacamoleProperties;
29 import net.sourceforge.guacamole.net.GuacamoleSocket;
30 import net.sourceforge.guacamole.servlet.GuacamoleSession;
31 import net.sourceforge.guacamole.net.GuacamoleTunnel;
32 import net.sourceforge.guacamole.net.auth.UserConfiguration;
33 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
34 import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
35 import net.sourceforge.guacamole.servlet.GuacamoleTunnelServlet;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class BasicGuacamoleTunnelServlet extends GuacamoleTunnelServlet {
40
41     private Logger logger = LoggerFactory.getLogger(BasicGuacamoleTunnelServlet.class);
42     
43     private AuthenticationProvider authProvider;
44
45     @Override
46     public void init() throws ServletException {
47
48         // Get auth provider instance
49         try {
50             authProvider = GuacamoleProperties.getProperty(BasicGuacamoleProperties.AUTH_PROVIDER);
51         }
52         catch (GuacamoleException e) {
53             logger.error("Error getting authentication provider from properties.", e);
54             throw new ServletException(e);
55         }
56
57     }
58
59     @Override
60     protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
61
62         HttpSession httpSession = request.getSession(true);
63
64         // Get ID of connection
65         String id = request.getParameter("id");
66         
67         // Get authorized configs
68         UserConfiguration userConfig = (UserConfiguration) 
69                 httpSession.getAttribute("GUAC_USER_CONFIG");
70
71         // If no configs in session, not authorized
72         if (userConfig == null)
73             throw new GuacamoleException("No authorized configurations.");
74
75         // Get authorized config
76         GuacamoleConfiguration config = userConfig.getConfiguration(id);
77         if (config == null) {
78             logger.error("Error retrieving authorized configuration id={}.", id);
79             throw new GuacamoleException("Unknown configuration ID.");
80         }
81         
82         logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
83
84         // Configure and connect socket
85         String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
86         int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
87
88         GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
89                 new InetGuacamoleSocket(hostname, port),
90                 config
91         );
92
93         // Associate socket with tunnel
94         GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
95
96         // Attach tunnel to session
97         GuacamoleSession session = new GuacamoleSession(httpSession);
98         session.attachTunnel(tunnel);
99
100         return tunnel;
101
102     }
103
104 }
105