diff --git a/guacamole-tunnel/src/main/java/org/openuds/guacamole/CredentialsServlet.java b/guacamole-tunnel/src/main/java/org/openuds/guacamole/CredentialsServlet.java deleted file mode 100644 index 47c7f1cf..00000000 --- a/guacamole-tunnel/src/main/java/org/openuds/guacamole/CredentialsServlet.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.openuds.guacamole; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.PrintWriter; -import org.openuds.guacamole.creds.Credentials; - -public class CredentialsServlet extends HttpServlet { - - /** - * - */ - private static final long serialVersionUID = 8321644141165009209L; - private static final String UUID_ERROR = "ERROR: Invalid UUID"; - private static final String PARAMS_ERROR = "ERROR: Invalid Credentials Parameters"; - private static final String OK = "OK"; - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - processCredentials(req, resp); - } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - processCredentials(req, resp); - } - - private void processCredentials(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - - resp.setContentType("text/plain"); - PrintWriter out = resp.getWriter(); - - String uuid = req.getParameter("uuid"); - String cred = req.getParameter("credential"); - String data = req.getParameter("data"); - - if( req.getParameter("test") != null && uuid != null ) { - if( Credentials.test(uuid) == false ) - out.println(UUID_ERROR); - else - out.println(OK); - return; - } - - if( uuid == null || cred == null || data == null ) { - out.println(PARAMS_ERROR); - return; - } - - // Test url: - // /creds?uuid=f070f721-15ea-44a9-8df1-b9480991989c&credential=12345&data=protocol%09rdp%0ahostname%09w7adolfo%0ausername%09admin%0apassword%09temporal - - if( Credentials.put(uuid, cred, data) == false ) - out.println(UUID_ERROR); - else - out.println(OK); - - } - -} diff --git a/guacamole-tunnel/src/main/java/org/openuds/guacamole/TunnelServlet.java b/guacamole-tunnel/src/main/java/org/openuds/guacamole/TunnelServlet.java index a0d7c669..f5618d5f 100644 --- a/guacamole-tunnel/src/main/java/org/openuds/guacamole/TunnelServlet.java +++ b/guacamole-tunnel/src/main/java/org/openuds/guacamole/TunnelServlet.java @@ -2,6 +2,7 @@ package org.openuds.guacamole; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Properties; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @@ -22,6 +23,24 @@ public class TunnelServlet * */ private static final long serialVersionUID = 2010742981126080080L; + private static final String UDS_PATH = "/guacamole/"; + + + private static Properties config = null; + + private String getConfigValue(String value) throws GuacamoleException { + if( config == null ) { + try { + config = new Properties(); + config.load(getServletContext().getResourceAsStream("/WEB-INF/tunnel.properties")); + } catch( Exception e ) { + throw new GuacamoleException(e.getMessage(), e); + } + } + + return config.getProperty(value); + + } @Override protected GuacamoleTunnel doConnect(HttpServletRequest request) @@ -34,7 +53,8 @@ public class TunnelServlet if( data == null || width == null || height == null) throw new GuacamoleException("Can't read required parameters"); - Hashtable params = Util.readParameters(data); + + Hashtable params = Util.readParameters( getConfigValue("uds") + UDS_PATH + data); if( params == null ) { System.out.println("Invalid credentials"); diff --git a/guacamole-tunnel/src/main/java/org/openuds/guacamole/Util.java b/guacamole-tunnel/src/main/java/org/openuds/guacamole/Util.java index f3ae9e98..c6425bf8 100644 --- a/guacamole-tunnel/src/main/java/org/openuds/guacamole/Util.java +++ b/guacamole-tunnel/src/main/java/org/openuds/guacamole/Util.java @@ -1,17 +1,22 @@ package org.openuds.guacamole; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Hashtable; -import org.openuds.guacamole.creds.Credentials; public class Util { // - public static Hashtable readParameters(String data) { + public static Hashtable readParameters(String url) { //String url = unscramble(data); //String params = getUrl(url); //return parseParams(params); - String params = Credentials.getAndRemove(data); - if( params == null ) + //String params = Credentials.getAndRemove(data); + String params = getUrl(url); + if( params == null || params.equals("ERROR")) return null; return parseParams(params); } @@ -30,4 +35,67 @@ public class Util { return res; } + + public static boolean download(String baseUrl, String id, String outputFileName) + { + try { + java.net.URL u = new java.net.URL(baseUrl + id); + java.net.URLConnection uc = u.openConnection(); + String contentType = uc.getContentType(); + int contentLength = uc.getContentLength(); + if (contentType.startsWith("text/") || contentLength == -1) { + throw new IOException("This is not a binary file."); + } + InputStream raw = uc.getInputStream(); + InputStream in = new BufferedInputStream(raw); + byte[] data = new byte[contentLength]; + int bytesRead = 0; + int offset = 0; + while (offset < contentLength) { + bytesRead = in.read(data, offset, data.length - offset); + if (bytesRead == -1) + break; + offset += bytesRead; + } + in.close(); + + if (offset != contentLength) { + throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); + } + + java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName); + out.write(data); + out.flush(); + out.close(); + + } catch(Exception e) { + System.out.println("Unable to download file, already present or network error? " + e.getMessage()); + return false; + } + return true; + } + + + public static String getUrl(String url) { + try { + java.net.URL u = new java.net.URL(url); + BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); + StringBuilder data = new StringBuilder(); + + String inputLine; + while ((inputLine = in.readLine()) != null) { + data.append(inputLine); + data.append("\n"); + } + + in.close(); + return data.toString(); + + } catch(Exception e) { + System.out.println("Unable to get url. Network error? " + e.getMessage()); + return null; + } + + } + } \ No newline at end of file diff --git a/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/Credentials.java b/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/Credentials.java deleted file mode 100644 index 887be276..00000000 --- a/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/Credentials.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.openuds.guacamole.creds; - -import java.util.LinkedHashMap; - -public class Credentials { - - private static CredentialsMap creds = new CredentialsMap(); - - public static boolean put(String uuid, String credential, String value) { - synchronized (creds) { - if( uuid.equals(creds.uniqueId) ) { - creds.put(credential, value); - return true; - } - return false; - } - } - - public static String get(String credential) { - synchronized (creds) { - return creds.get(credential); - } - } - - public static String getAndRemove(String credential) { - synchronized (creds) { - String cred = creds.get(credential); - creds.put(credential, null); - return cred; - - } - } - - public static boolean test(String uuid) { - synchronized (creds) { - if( uuid.equals(creds.uniqueId) ) - return true; - return false; - } - - } - - -} - diff --git a/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/CredentialsMap.java b/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/CredentialsMap.java deleted file mode 100644 index 81e8d1d9..00000000 --- a/guacamole-tunnel/src/main/java/org/openuds/guacamole/creds/CredentialsMap.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.openuds.guacamole.creds; - -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.util.LinkedHashMap; - -public class CredentialsMap extends LinkedHashMap { - - private static final int MAX_CREDENTIALS = 1024; - public String uniqueId; - - public CredentialsMap() { - super(MAX_CREDENTIALS); - try { - FileInputStream fi = new FileInputStream("/etc/uniqueid.cfg"); - DataInputStream in = new DataInputStream(fi); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - uniqueId = br.readLine(); - in.close(); - } catch(Exception e) { - uniqueId = null; - } - } - - @Override - protected boolean removeEldestEntry( - java.util.Map.Entry eldest) { - return size() >= MAX_CREDENTIALS; - } -} diff --git a/guacamole-tunnel/src/main/webapp/WEB-INF/tunnel.properties b/guacamole-tunnel/src/main/webapp/WEB-INF/tunnel.properties new file mode 100644 index 00000000..c6faa463 --- /dev/null +++ b/guacamole-tunnel/src/main/webapp/WEB-INF/tunnel.properties @@ -0,0 +1 @@ +uds=http://172.27.0.1:8000 diff --git a/server/src/uds/auths/IP/Authenticator.py b/server/src/uds/auths/IP/Authenticator.py index 826c3dd3..fdbc9062 100644 --- a/server/src/uds/auths/IP/Authenticator.py +++ b/server/src/uds/auths/IP/Authenticator.py @@ -109,7 +109,7 @@ class IPAuth(Authenticator): # doAutoLogin = Config.section('IPAUTH').value('autoLogin', '0').getBool() gm = GroupsManager(self.dbAuthenticator()) self.getGroups(request.ip, gm) - if len(gm.getValidGroups()) > 0 and self.dbAuthenticator().isValidUser(request.ip, True): + if gm.hasValidGroups() and self.dbAuthenticator().isValidUser(request.ip, True): passw = ''.join(random.choice(string.letters + string.digits) for __ in xrange(12)) self.cache().put(request.ip, passw) return ''