1 package net.sourceforge.guacamole.net.basic;
4 * Guacamole - Clientless Remote Desktop
5 * Copyright (C) 2010 Michael Jumper
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.
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.
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/>.
21 import java.lang.reflect.InvocationTargetException;
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.GuacamoleTCPClient;
27 import net.sourceforge.guacamole.net.Configuration;
28 import net.sourceforge.guacamole.net.GuacamoleProperties;
29 import net.sourceforge.guacamole.net.GuacamoleSession;
30 import net.sourceforge.guacamole.net.tunnel.GuacamoleTunnel;
31 import net.sourceforge.guacamole.net.tunnel.GuacamoleTunnelServlet;
33 public class BasicGuacamoleTunnelServlet extends GuacamoleTunnelServlet {
35 private AuthenticationProvider authProvider;
38 public void init() throws ServletException {
40 // Get auth provider instance
42 String authProviderClassName = GuacamoleProperties.getProperty("auth-provider");
43 Object obj = Class.forName(authProviderClassName).getConstructor().newInstance();
44 if (!(obj instanceof AuthenticationProvider))
45 throw new ServletException("Specified authentication provider class is not a AuthenticationProvider.");
47 authProvider = (AuthenticationProvider) obj;
49 catch (GuacamoleException e) {
50 throw new ServletException(e);
52 catch (ClassNotFoundException e) {
53 throw new ServletException("Authentication provider class not found", e);
55 catch (NoSuchMethodException e) {
56 throw new ServletException("Default constructor for authentication provider not present", e);
58 catch (SecurityException e) {
59 throw new ServletException("Creation of authentication provider disallowed; check your security settings", e);
61 catch (InstantiationException e) {
62 throw new ServletException("Unable to instantiate authentication provider", e);
64 catch (IllegalAccessException e) {
65 throw new ServletException("Unable to access default constructor of authentication provider", e);
67 catch (InvocationTargetException e) {
68 throw new ServletException("Internal error in constructor of authentication provider", e.getTargetException());
74 protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
76 HttpSession httpSession = request.getSession(true);
78 // Retrieve username and password from parms
79 String username = request.getParameter("username");
80 String password = request.getParameter("password");
82 // Get authorized config
83 Configuration config = authProvider.getAuthorizedConfiguration(username, password);
85 throw new GuacamoleException("Invalid login");
87 // Configure and connect client
88 String hostname = GuacamoleProperties.getProperty("guacd-hostname");
89 int port = GuacamoleProperties.getIntProperty("guacd-port", null);
91 GuacamoleTCPClient client = new GuacamoleTCPClient(hostname, port);
92 client.connect(config);
94 // Associate client with tunnel
95 GuacamoleTunnel tunnel = new GuacamoleTunnel(client);
97 // Attach tunnel to session
98 GuacamoleSession session = new GuacamoleSession(httpSession);
99 session.attachTunnel(tunnel);