2 package net.sourceforge.guacamole.net.basic;
5 * Guacamole - Clientless Remote Desktop
6 * Copyright (C) 2010 Michael Jumper
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 import java.io.IOException;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.Collections;
27 import java.util.HashMap;
29 import net.sourceforge.guacamole.GuacamoleException;
30 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
31 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
32 import net.sourceforge.guacamole.properties.GuacamoleProperties;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35 import org.xml.sax.XMLReader;
36 import org.xml.sax.helpers.DefaultHandler;
37 import org.xml.sax.helpers.XMLReaderFactory;
39 public class BasicFileAuthenticationProvider implements AuthenticationProvider {
41 private long mappingTime;
42 private Map<String, AuthInfo> mapping;
44 private File getUserMappingFile() throws GuacamoleException {
46 // Get user mapping file
47 return GuacamoleProperties.getProperty(BasicGuacamoleProperties.BASIC_USER_MAPPING);
51 public synchronized void init() throws GuacamoleException {
53 // Get user mapping file
54 File mapFile = getUserMappingFile();
56 throw new GuacamoleException("Missing \"basic-user-mapping\" parameter required for basic login.");
61 BasicUserMappingContentHandler contentHandler = new BasicUserMappingContentHandler();
63 XMLReader parser = XMLReaderFactory.createXMLReader();
64 parser.setContentHandler(contentHandler);
65 parser.parse(mapFile.getAbsolutePath());
67 mappingTime = mapFile.lastModified();
68 mapping = contentHandler.getUserMapping();
71 catch (IOException e) {
72 throw new GuacamoleException("Error reading basic user mapping file.", e);
74 catch (SAXException e) {
75 throw new GuacamoleException("Error parsing basic user mapping XML.", e);
81 public GuacamoleConfiguration getAuthorizedConfiguration(String username, String password) throws GuacamoleException {
83 // Check mapping file mod time
84 File userMappingFile = getUserMappingFile();
85 if (userMappingFile.exists() && mappingTime < userMappingFile.lastModified()) {
87 // If modified recently, gain exclusive access and recheck
89 if (userMappingFile.exists() && mappingTime < userMappingFile.lastModified())
90 init(); // If still not up to date, re-init
95 AuthInfo info = mapping.get(username);
96 if (info != null && info.validate(username, password))
97 return info.getConfiguration();
103 public static class AuthInfo {
105 public static enum Encoding {
110 private String auth_username;
111 private String auth_password;
112 private Encoding auth_encoding;
114 private GuacamoleConfiguration config;
116 public AuthInfo(String auth_username, String auth_password, Encoding auth_encoding) {
117 this.auth_username = auth_username;
118 this.auth_password = auth_password;
119 this.auth_encoding = auth_encoding;
121 config = new GuacamoleConfiguration();
124 private static final char HEX_CHARS[] = {
125 '0', '1', '2', '3', '4', '5', '6', '7',
126 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
129 public static String getHexString(byte[] bytes) {
134 StringBuilder hex = new StringBuilder(2 * bytes.length);
135 for (byte b : bytes) {
136 hex.append(HEX_CHARS[(b & 0xF0) >> 4])
137 .append(HEX_CHARS[(b & 0x0F) ]);
140 return hex.toString();
145 public boolean validate(String username, String password) {
147 // If username matches
148 if (username != null && password != null && username.equals(auth_username)) {
150 switch (auth_encoding) {
155 return password.equals(auth_password);
159 // Compare hashed password
161 MessageDigest digest = MessageDigest.getInstance("MD5");
162 String hashedPassword = getHexString(digest.digest(password.getBytes()));
163 return hashedPassword.equals(auth_password.toUpperCase());
165 catch (NoSuchAlgorithmException e) {
166 throw new UnsupportedOperationException("Unexpected lack of MD5 support.", e);
177 public GuacamoleConfiguration getConfiguration() {
184 private static class BasicUserMappingContentHandler extends DefaultHandler {
186 private Map<String, AuthInfo> authMapping = new HashMap<String, AuthInfo>();
188 public Map<String, AuthInfo> getUserMapping() {
189 return Collections.unmodifiableMap(authMapping);
201 private State state = State.ROOT;
202 private AuthInfo current = null;
203 private String currentParameter = null;
206 public void endElement(String uri, String localName, String qName) throws SAXException {
212 if (localName.equals("user-mapping")) {
221 if (localName.equals("authorize")) {
223 // Finalize mapping for this user
225 current.auth_username,
229 state = State.USER_MAPPING;
237 if (localName.equals("protocol")) {
238 state = State.AUTH_INFO;
246 if (localName.equals("param")) {
247 state = State.AUTH_INFO;
255 throw new SAXException("Tag not yet complete: " + localName);
260 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
264 // Document must be <user-mapping>
267 if (localName.equals("user-mapping")) {
268 state = State.USER_MAPPING;
274 // Only <authorize> tags allowed in main document
277 if (localName.equals("authorize")) {
279 AuthInfo.Encoding encoding;
280 String encodingString = attributes.getValue("encoding");
281 if (encodingString == null)
282 encoding = AuthInfo.Encoding.PLAIN_TEXT;
283 else if (encodingString.equals("plain"))
284 encoding = AuthInfo.Encoding.PLAIN_TEXT;
285 else if (encodingString.equals("md5"))
286 encoding = AuthInfo.Encoding.MD5;
288 throw new SAXException("Invalid encoding type");
291 current = new AuthInfo(
292 attributes.getValue("username"),
293 attributes.getValue("password"),
298 state = State.AUTH_INFO;
306 if (localName.equals("protocol")) {
308 state = State.PROTOCOL;
312 if (localName.equals("param")) {
314 currentParameter = attributes.getValue("name");
315 if (currentParameter == null)
316 throw new SAXException("Attribute \"name\" required for param tag.");
319 state = State.PARAMETER;
327 throw new SAXException("Unexpected tag: " + localName);
332 public void characters(char[] ch, int start, int length) throws SAXException {
334 String str = new String(ch, start, length);
338 current.getConfiguration().setProtocol(str);
342 current.getConfiguration().setParameter(currentParameter, str);
347 if (str.trim().length() != 0)
348 throw new SAXException("Unexpected character data.");