src/org/sonews/acl/AuthInfoCommand.java
author František Kučera <franta-hg@frantovo.cz>
Mon Nov 07 17:35:43 2011 +0100 (2011-11-07)
changeset 117 79ce65d63cce
parent 101 d54786065fa3
child 118 ba7ea56fd672
permissions -rw-r--r--
ACL: uživatel v Article bude jako User ne jen jako String.
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@101
    52
		return false;
franta-hg@101
    53
	}
franta-hg@101
    54
franta-hg@101
    55
	@Override
franta-hg@101
    56
	public String[] getSupportedCommandStrings() {
franta-hg@101
    57
		return SUPPORTED_COMMANDS;
franta-hg@101
    58
	}
franta-hg@101
    59
franta-hg@101
    60
	@Override
franta-hg@101
    61
	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
franta-hg@101
    62
		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
franta-hg@101
    63
		Matcher commandMatcher = commandPattern.matcher(line);
franta-hg@101
    64
franta-hg@101
    65
		if (commandMatcher.matches()) {
franta-hg@101
    66
franta-hg@112
    67
			if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
franta-hg@101
    68
				conn.println("502 Command unavailable (you are already authenticated)");
franta-hg@101
    69
			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@112
    70
				conn.setUser(new User(commandMatcher.group(2)));
franta-hg@112
    71
				conn.println("381 Password required"); // ask user for his password
franta-hg@112
    72
				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUser().getUserName());
franta-hg@101
    73
			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@112
    74
				if (conn.getUser() == null) {
franta-hg@101
    75
					conn.println("482 Authentication commands issued out of sequence");
franta-hg@101
    76
				} else {
franta-hg@101
    77
franta-hg@101
    78
					char[] password = commandMatcher.group(2).toCharArray();
franta-hg@112
    79
					boolean goodPassword = StorageManager.current().authenticateUser(conn.getUser().getUserName(), password);
franta-hg@101
    80
					Arrays.fill(password, '*');
franta-hg@101
    81
					commandMatcher = null;
franta-hg@101
    82
franta-hg@101
    83
					if (goodPassword) {
franta-hg@101
    84
						conn.println("281 Authentication accepted");
franta-hg@112
    85
						conn.getUser().setAuthenticated(true);
franta-hg@112
    86
						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUser().getUserName());
franta-hg@101
    87
					} else {
franta-hg@112
    88
						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUser().getUserName());
franta-hg@112
    89
						conn.setUser(null);
franta-hg@101
    90
						conn.println("481 Authentication failed: wrong password");
franta-hg@101
    91
					}
franta-hg@101
    92
franta-hg@101
    93
				}
franta-hg@101
    94
			} else {
franta-hg@101
    95
				// impossible, see commandPattern
franta-hg@101
    96
				conn.println("500 Unknown command");
franta-hg@101
    97
			}
franta-hg@101
    98
franta-hg@101
    99
franta-hg@101
   100
		} else {
franta-hg@101
   101
			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
franta-hg@101
   102
		}
franta-hg@101
   103
	}
franta-hg@101
   104
}