src/org/sonews/acl/DrupalAuthInfoCommand.java
author František Kučera <franta-hg@frantovo.cz>
Wed Oct 19 21:40:51 2011 +0200 (2011-10-19)
changeset 101 d54786065fa3
permissions -rw-r--r--
Drupal: ověřování uživatelů.
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
import org.sonews.storage.StorageProvider;
franta-hg@101
    31
import org.sonews.storage.impl.DrupalDatabaseProvider;
franta-hg@101
    32
franta-hg@101
    33
/**
franta-hg@101
    34
 *
franta-hg@101
    35
 * @author František Kučera (frantovo.cz)
franta-hg@101
    36
 */
franta-hg@101
    37
public class DrupalAuthInfoCommand implements Command {
franta-hg@101
    38
franta-hg@101
    39
	private static final Logger log = Logger.getLogger(DrupalAuthInfoCommand.class.getName());
franta-hg@101
    40
	private static String[] SUPPORTED_COMMANDS = {"AUTHINFO"};
franta-hg@101
    41
franta-hg@101
    42
	@Override
franta-hg@101
    43
	public boolean hasFinished() {
franta-hg@101
    44
		return true;
franta-hg@101
    45
	}
franta-hg@101
    46
franta-hg@101
    47
	@Override
franta-hg@101
    48
	public String impliedCapability() {
franta-hg@101
    49
		return "AUTHINFO";
franta-hg@101
    50
	}
franta-hg@101
    51
franta-hg@101
    52
	@Override
franta-hg@101
    53
	public boolean isStateful() {
franta-hg@101
    54
		return false;
franta-hg@101
    55
	}
franta-hg@101
    56
franta-hg@101
    57
	@Override
franta-hg@101
    58
	public String[] getSupportedCommandStrings() {
franta-hg@101
    59
		return SUPPORTED_COMMANDS;
franta-hg@101
    60
	}
franta-hg@101
    61
franta-hg@101
    62
	@Override
franta-hg@101
    63
	public void processLine(NNTPConnection conn, String line, byte[] rawLine) throws IOException, StorageBackendException {
franta-hg@101
    64
		Pattern commandPattern = Pattern.compile("AUTHINFO (USER|PASS) (.*)", Pattern.CASE_INSENSITIVE);
franta-hg@101
    65
		Matcher commandMatcher = commandPattern.matcher(line);
franta-hg@101
    66
franta-hg@101
    67
		if (commandMatcher.matches()) {
franta-hg@101
    68
franta-hg@101
    69
			if (conn.isUserAuthenticated()) {
franta-hg@101
    70
				conn.println("502 Command unavailable (you are already authenticated)");
franta-hg@101
    71
			} else if ("USER".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@101
    72
				conn.setUsername(commandMatcher.group(2));
franta-hg@101
    73
				conn.println("381 Password required");
franta-hg@101
    74
				log.log(Level.FINE, "User ''{0}'' greets us. We are waiting for his password.", conn.getUsername());
franta-hg@101
    75
			} else if ("PASS".equalsIgnoreCase(commandMatcher.group(1))) {
franta-hg@101
    76
				if (conn.getUsername() == null) {
franta-hg@101
    77
					conn.println("482 Authentication commands issued out of sequence");
franta-hg@101
    78
				} else {
franta-hg@101
    79
franta-hg@101
    80
					char[] password = commandMatcher.group(2).toCharArray();
franta-hg@101
    81
					boolean goodPassword = StorageManager.current().authenticateUser(conn.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@101
    87
						conn.setUserAuthenticated(true);
franta-hg@101
    88
						log.log(Level.INFO, "User ''{0}'' has been succesfully authenticated.", conn.getUsername());
franta-hg@101
    89
					} else {
franta-hg@101
    90
						log.log(Level.INFO, "User ''{0}'' has provided wrong password.", conn.getUsername());
franta-hg@101
    91
						conn.setUsername(null);
franta-hg@101
    92
						conn.setUserAuthenticated(false);
franta-hg@101
    93
						conn.println("481 Authentication failed: wrong password");
franta-hg@101
    94
					}
franta-hg@101
    95
franta-hg@101
    96
				}
franta-hg@101
    97
			} else {
franta-hg@101
    98
				// impossible, see commandPattern
franta-hg@101
    99
				conn.println("500 Unknown command");
franta-hg@101
   100
			}
franta-hg@101
   101
franta-hg@101
   102
franta-hg@101
   103
		} else {
franta-hg@101
   104
			conn.println("500 Unknown command, expecting AUTHINFO USER username or AUTHINFO PASS password ");
franta-hg@101
   105
		}
franta-hg@101
   106
	}
franta-hg@101
   107
}