src/org/sonews/acl/DrupalAuthInfoCommand.java
changeset 101 d54786065fa3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/acl/DrupalAuthInfoCommand.java	Wed Oct 19 21:40:51 2011 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package org.sonews.acl;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.util.Arrays;
    1.25 +import java.util.logging.Level;
    1.26 +import java.util.logging.Logger;
    1.27 +import java.util.regex.Matcher;
    1.28 +import java.util.regex.Pattern;
    1.29 +import org.sonews.daemon.NNTPConnection;
    1.30 +import org.sonews.daemon.command.Command;
    1.31 +import org.sonews.storage.StorageBackendException;
    1.32 +import org.sonews.storage.StorageManager;
    1.33 +import org.sonews.storage.StorageProvider;
    1.34 +import org.sonews.storage.impl.DrupalDatabaseProvider;
    1.35 +
    1.36 +/**
    1.37 + *
    1.38 + * @author František Kučera (frantovo.cz)
    1.39 + */
    1.40 +public class DrupalAuthInfoCommand implements Command {
    1.41 +
    1.42 +	private static final Logger log = Logger.getLogger(DrupalAuthInfoCommand.class.getName());
    1.43 +	private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
    1.44 +
    1.45 +	@Override
    1.46 +	public boolean hasFinished() {
    1.47 +		return true;
    1.48 +	}
    1.49 +
    1.50 +	@Override
    1.51 +	public String impliedCapability() {
    1.52 +		return "AUTHINFO";
    1.53 +	}
    1.54 +
    1.55 +	@Override
    1.56 +	public boolean isStateful() {
    1.57 +		return false;
    1.58 +	}
    1.59 +
    1.60 +	@Override
    1.61 +	public String[] getSupportedCommandStrings() {
    1.62 +		return SUPPORTED_COMMANDS;
    1.63 +	}
    1.64 +
    1.65 +	@Override
    1.66 +	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
    1.67 +		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
    1.68 +		Matcher commandMatcher = commandPattern.matcher(line);
    1.69 +
    1.70 +		if (commandMatcher.matches()) {
    1.71 +
    1.72 +			if (conn.isUserAuthenticated()) {
    1.73 +				conn.println("502 Command unavailable (you are already authenticated)");
    1.74 +			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
    1.75 +				conn.setUsername(commandMatcher.group(2));
    1.76 +				conn.println("381 Password required");
    1.77 +				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUsername());
    1.78 +			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
    1.79 +				if (conn.getUsername() == null) {
    1.80 +					conn.println("482 Authentication commands issued out of sequence");
    1.81 +				} else {
    1.82 +
    1.83 +					char[] password = commandMatcher.group(2).toCharArray();
    1.84 +					boolean goodPassword = StorageManager.current().authenticateUser(conn.getUsername(), password);
    1.85 +					Arrays.fill(password, '*');
    1.86 +					commandMatcher = null;
    1.87 +
    1.88 +					if (goodPassword) {
    1.89 +						conn.println("281 Authentication accepted");
    1.90 +						conn.setUserAuthenticated(true);
    1.91 +						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUsername());
    1.92 +					} else {
    1.93 +						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUsername());
    1.94 +						conn.setUsername(null);
    1.95 +						conn.setUserAuthenticated(false);
    1.96 +						conn.println("481 Authentication failed: wrong password");
    1.97 +					}
    1.98 +
    1.99 +				}
   1.100 +			} else {
   1.101 +				// impossible, see commandPattern
   1.102 +				conn.println("500 Unknown command");
   1.103 +			}
   1.104 +
   1.105 +
   1.106 +		} else {
   1.107 +			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
   1.108 +		}
   1.109 +	}
   1.110 +}