Upgraded 1.1 tag

This commit is contained in:
Adolfo Gómez 2013-03-19 15:45:43 +00:00
commit a9270a560a
7 changed files with 95 additions and 150 deletions

View File

@ -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);
}
}

View File

@ -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<String,String> params = Util.readParameters(data);
Hashtable<String,String> params = Util.readParameters( getConfigValue("uds") + UDS_PATH + data);
if( params == null ) {
System.out.println("Invalid credentials");

View File

@ -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<String,String> readParameters(String data) {
public static Hashtable<String,String> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<String, String> {
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<String, String> eldest) {
return size() >= MAX_CREDENTIALS;
}
}

View File

@ -0,0 +1 @@
uds=http://172.27.0.1:8000

View File

@ -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 '<script type="text/javascript">$("#id_user").val("' + request.ip + '");$("#id_password").val("' + passw + '");$("#loginform").submit();</script>'