franta-hg@101: /* franta-hg@101: * SONEWS News Server franta-hg@101: * see AUTHORS for the list of contributors franta-hg@101: * franta-hg@101: * This program is free software: you can redistribute it and/or modify franta-hg@101: * it under the terms of the GNU General Public License as published by franta-hg@101: * the Free Software Foundation, either version 3 of the License, or franta-hg@101: * (at your option) any later version. franta-hg@101: * franta-hg@101: * This program is distributed in the hope that it will be useful, franta-hg@101: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@101: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@101: * GNU General Public License for more details. franta-hg@101: * franta-hg@101: * You should have received a copy of the GNU General Public License franta-hg@101: * along with this program. If not, see . franta-hg@101: */ franta-hg@101: package org.sonews.acl; franta-hg@101: franta-hg@101: import java.io.IOException; franta-hg@101: import java.util.Arrays; franta-hg@101: import java.util.logging.Level; franta-hg@101: import java.util.logging.Logger; franta-hg@101: import java.util.regex.Matcher; franta-hg@101: import java.util.regex.Pattern; franta-hg@101: import org.sonews.daemon.NNTPConnection; franta-hg@101: import org.sonews.daemon.command.Command; franta-hg@101: import org.sonews.storage.StorageBackendException; franta-hg@101: import org.sonews.storage.StorageManager; franta-hg@101: franta-hg@101: /** franta-hg@101: * franta-hg@101: * @author František Kučera (frantovo.cz) franta-hg@101: */ franta-hg@112: public class AuthInfoCommand implements Command { franta-hg@101: franta-hg@112: private static final Logger log = Logger.getLogger(AuthInfoCommand.class.getName()); franta-hg@101: private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"}; franta-hg@101: franta-hg@101: @Override franta-hg@101: public boolean hasFinished() { franta-hg@101: return true; franta-hg@101: } franta-hg@101: franta-hg@101: @Override franta-hg@101: public String impliedCapability() { franta-hg@101: return "AUTHINFO"; franta-hg@101: } franta-hg@101: franta-hg@101: @Override franta-hg@101: public boolean isStateful() { franta-hg@118: // TODO: make it statefull? franta-hg@101: return false; franta-hg@101: } franta-hg@101: franta-hg@101: @Override franta-hg@101: public String[] getSupportedCommandStrings() { franta-hg@101: return SUPPORTED_COMMANDS; franta-hg@101: } franta-hg@101: franta-hg@101: @Override franta-hg@101: public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException { franta-hg@101: Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE); franta-hg@101: Matcher commandMatcher = commandPattern.matcher(line); franta-hg@101: franta-hg@101: if (commandMatcher.matches()) { franta-hg@101: franta-hg@112: if (conn.getUser() != null && conn.getUser().isAuthenticated()) { franta-hg@101: conn.println("502 Command unavailable (you are already authenticated)"); franta-hg@101: } else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) { franta-hg@112: conn.setUser(new User(commandMatcher.group(2))); franta-hg@112: conn.println("381 Password required"); // ask user for his password franta-hg@112: log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUser().getUserName()); franta-hg@101: } else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) { franta-hg@112: if (conn.getUser() == null) { franta-hg@101: conn.println("482 Authentication commands issued out of sequence"); franta-hg@101: } else { franta-hg@101: franta-hg@101: char[] password = commandMatcher.group(2).toCharArray(); franta-hg@118: // TODO: StorageManager should return User object instead of boolean (so there could be transferred some additional information about user) franta-hg@112: boolean goodPassword = StorageManager.current().authenticateUser(conn.getUser().getUserName(), password); franta-hg@101: Arrays.fill(password, '*'); franta-hg@101: commandMatcher = null; franta-hg@101: franta-hg@101: if (goodPassword) { franta-hg@101: conn.println("281 Authentication accepted"); franta-hg@112: conn.getUser().setAuthenticated(true); franta-hg@112: log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUser().getUserName()); franta-hg@101: } else { franta-hg@112: log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUser().getUserName()); franta-hg@112: conn.setUser(null); franta-hg@101: conn.println("481 Authentication failed: wrong password"); franta-hg@101: } franta-hg@101: franta-hg@101: } franta-hg@101: } else { franta-hg@101: // impossible, see commandPattern franta-hg@101: conn.println("500 Unknown command"); franta-hg@101: } franta-hg@101: franta-hg@101: franta-hg@101: } else { franta-hg@101: conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password "); franta-hg@101: } franta-hg@101: } franta-hg@101: }