1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/acl/AuthInfoCommand.java Sun Oct 30 22:13:32 2011 +0100
1.3 @@ -0,0 +1,104 @@
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 +
1.34 +/**
1.35 + *
1.36 + * @author František Kučera (frantovo.cz)
1.37 + */
1.38 +public class AuthInfoCommand implements Command {
1.39 +
1.40 + private static final Logger log = Logger.getLogger(AuthInfoCommand.class.getName());
1.41 + private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
1.42 +
1.43 + @Override
1.44 + public boolean hasFinished() {
1.45 + return true;
1.46 + }
1.47 +
1.48 + @Override
1.49 + public String impliedCapability() {
1.50 + return "AUTHINFO";
1.51 + }
1.52 +
1.53 + @Override
1.54 + public boolean isStateful() {
1.55 + return false;
1.56 + }
1.57 +
1.58 + @Override
1.59 + public String[] getSupportedCommandStrings() {
1.60 + return SUPPORTED_COMMANDS;
1.61 + }
1.62 +
1.63 + @Override
1.64 + public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
1.65 + Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
1.66 + Matcher commandMatcher = commandPattern.matcher(line);
1.67 +
1.68 + if (commandMatcher.matches()) {
1.69 +
1.70 + if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
1.71 + conn.println("502 Command unavailable (you are already authenticated)");
1.72 + } else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
1.73 + conn.setUser(new User(commandMatcher.group(2)));
1.74 + conn.println("381 Password required"); // ask user for his password
1.75 + log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUser().getUserName());
1.76 + } else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
1.77 + if (conn.getUser() == null) {
1.78 + conn.println("482 Authentication commands issued out of sequence");
1.79 + } else {
1.80 +
1.81 + char[] password = commandMatcher.group(2).toCharArray();
1.82 + boolean goodPassword = StorageManager.current().authenticateUser(conn.getUser().getUserName(), password);
1.83 + Arrays.fill(password, '*');
1.84 + commandMatcher = null;
1.85 +
1.86 + if (goodPassword) {
1.87 + conn.println("281 Authentication accepted");
1.88 + conn.getUser().setAuthenticated(true);
1.89 + log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUser().getUserName());
1.90 + } else {
1.91 + log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUser().getUserName());
1.92 + conn.setUser(null);
1.93 + conn.println("481 Authentication failed: wrong password");
1.94 + }
1.95 +
1.96 + }
1.97 + } else {
1.98 + // impossible, see commandPattern
1.99 + conn.println("500 Unknown command");
1.100 + }
1.101 +
1.102 +
1.103 + } else {
1.104 + conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
1.105 + }
1.106 + }
1.107 +}