src/org/sonews/acl/AuthInfoCommand.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 07 Nov 2011 17:47:10 +0100
changeset 118 ba7ea56fd672
parent 112 ca54040b4409
permissions -rw-r--r--
drobnosti, TODO
franta-hg@101
     1
/*
franta-hg@101
     2
 *   SONEWS News Server
franta-hg@101
     3
 *   see AUTHORS for the list of contributors
franta-hg@101
     4
 *
franta-hg@101
     5
 *   This program is free software: you can redistribute it and/or modify
franta-hg@101
     6
 *   it under the terms of the GNU General Public License as published by
franta-hg@101
     7
 *   the Free Software Foundation, either version 3 of the License, or
franta-hg@101
     8
 *   (at your option) any later version.
franta-hg@101
     9
 *
franta-hg@101
    10
 *   This program is distributed in the hope that it will be useful,
franta-hg@101
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@101
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@101
    13
 *   GNU General Public License for more details.
franta-hg@101
    14
 *
franta-hg@101
    15
 *   You should have received a copy of the GNU General Public License
franta-hg@101
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@101
    17
 */
franta-hg@101
    18
package org.sonews.acl;
franta-hg@101
    19
franta-hg@101
    20
import java.io.IOException;
franta-hg@101
    21
import java.util.Arrays;
franta-hg@101
    22
import java.util.logging.Level;
franta-hg@101
    23
import java.util.logging.Logger;
franta-hg@101
    24
import java.util.regex.Matcher;
franta-hg@101
    25
import java.util.regex.Pattern;
franta-hg@101
    26
import org.sonews.daemon.NNTPConnection;
franta-hg@101
    27
import org.sonews.daemon.command.Command;
franta-hg@101
    28
import org.sonews.storage.StorageBackendException;
franta-hg@101
    29
import org.sonews.storage.StorageManager;
franta-hg@101
    30
franta-hg@101
    31
/**
franta-hg@101
    32
 *
franta-hg@101
    33
 * @author František Kučera (frantovo.cz)
franta-hg@101
    34
 */
franta-hg@112
    35
public class AuthInfoCommand implements Command {
franta-hg@101
    36
franta-hg@112
    37
	private static final Logger log = Logger.getLogger(AuthInfoCommand.class.getName());
franta-hg@101
    38
	private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
franta-hg@101
    39
franta-hg@101
    40
	@Override
franta-hg@101
    41
	public boolean hasFinished() {
franta-hg@101
    42
		return true;
franta-hg@101
    43
	}
franta-hg@101
    44
franta-hg@101
    45
	@Override
franta-hg@101
    46
	public String impliedCapability() {
franta-hg@101
    47
		return "AUTHINFO";
franta-hg@101
    48
	}
franta-hg@101
    49
franta-hg@101
    50
	@Override
franta-hg@101
    51
	public boolean isStateful() {
franta-hg@118
    52
		// TODO: make it statefull?
franta-hg@101
    53
		return false;
franta-hg@101
    54
	}
franta-hg@101
    55
franta-hg@101
    56
	@Override
franta-hg@101
    57
	public String[] getSupportedCommandStrings() {
franta-hg@101
    58
		return SUPPORTED_COMMANDS;
franta-hg@101
    59
	}
franta-hg@101
    60
franta-hg@101
    61
	@Override
franta-hg@101
    62
	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
franta-hg@101
    63
		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
franta-hg@101
    64
		Matcher commandMatcher = commandPattern.matcher(line);
franta-hg@101
    65
franta-hg@101
    66
		if (commandMatcher.matches()) {
franta-hg@101
    67
franta-hg@112
    68
			if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
franta-hg@101
    69
				conn.println("502 Command unavailable (you are already authenticated)");
franta-hg@101
    70
			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@112
    71
				conn.setUser(new User(commandMatcher.group(2)));
franta-hg@112
    72
				conn.println("381 Password required"); // ask user for his password
franta-hg@112
    73
				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUser().getUserName());
franta-hg@101
    74
			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@112
    75
				if (conn.getUser() == null) {
franta-hg@101
    76
					conn.println("482 Authentication commands issued out of sequence");
franta-hg@101
    77
				} else {
franta-hg@101
    78
franta-hg@101
    79
					char[] password = commandMatcher.group(2).toCharArray();
franta-hg@118
    80
					// TODO: StorageManager should return User object instead of boolean (so there could be transferred some additional information about user)
franta-hg@112
    81
					boolean goodPassword = StorageManager.current().authenticateUser(conn.getUser().getUserName(), password);
franta-hg@101
    82
					Arrays.fill(password, '*');
franta-hg@101
    83
					commandMatcher = null;
franta-hg@101
    84
franta-hg@101
    85
					if (goodPassword) {
franta-hg@101
    86
						conn.println("281 Authentication accepted");
franta-hg@112
    87
						conn.getUser().setAuthenticated(true);
franta-hg@112
    88
						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUser().getUserName());
franta-hg@101
    89
					} else {
franta-hg@112
    90
						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUser().getUserName());
franta-hg@112
    91
						conn.setUser(null);
franta-hg@101
    92
						conn.println("481 Authentication failed: wrong password");
franta-hg@101
    93
					}
franta-hg@101
    94
franta-hg@101
    95
				}
franta-hg@101
    96
			} else {
franta-hg@101
    97
				// impossible, see commandPattern
franta-hg@101
    98
				conn.println("500 Unknown command");
franta-hg@101
    99
			}
franta-hg@101
   100
franta-hg@101
   101
franta-hg@101
   102
		} else {
franta-hg@101
   103
			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
franta-hg@101
   104
		}
franta-hg@101
   105
	}
franta-hg@101
   106
}