Working multiple-config login stub.
[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.util.Map;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.http.HttpSession;
26 import net.sourceforge.guacamole.GuacamoleException;
27 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
28 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
29 import net.sourceforge.guacamole.properties.GuacamoleProperties;
30 import net.sourceforge.guacamole.net.GuacamoleSocket;
31 import net.sourceforge.guacamole.servlet.GuacamoleSession;
32 import net.sourceforge.guacamole.net.GuacamoleTunnel;
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         Map<String, GuacamoleConfiguration> configs =
69                 (Map<String, GuacamoleConfiguration>)
70                 httpSession.getAttribute("GUAC_AUTH_CONFIGS");
71
72         // If no configs in session, not authorized
73         if (configs == null)
74             throw new GuacamoleException("No authorized configurations.");
75
76         // Get authorized config
77         GuacamoleConfiguration config = configs.get(id);
78         if (config == null) {
79             logger.error("Error retrieving authorized configuration id={}.", id);
80             throw new GuacamoleException("Unknown configuration ID.");
81         }
82         
83         logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
84
85         // Configure and connect socket
86         String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
87         int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
88
89         GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
90                 new InetGuacamoleSocket(hostname, port),
91                 config
92         );
93
94         // Associate socket with tunnel
95         GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
96
97         // Attach tunnel to session
98         GuacamoleSession session = new GuacamoleSession(httpSession);
99         session.attachTunnel(tunnel);
100
101         return tunnel;
102
103     }
104
105 }
106