1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.UnknownHostException ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import net.sf.drftpd.HostMask; 27 import net.sf.drftpd.event.UserEvent; 28 import net.sf.drftpd.master.BaseFtpConnection; 29 import net.sf.drftpd.master.FtpReply; 30 import net.sf.drftpd.master.FtpRequest; 31 import net.sf.drftpd.master.command.CommandManager; 32 import net.sf.drftpd.master.command.CommandManagerFactory; 33 import net.sf.drftpd.master.usermanager.NoSuchUserException; 34 import net.sf.drftpd.master.usermanager.User; 35 import net.sf.drftpd.master.usermanager.UserFileException; 36 37 import org.apache.log4j.Logger; 38 import org.drftpd.commands.CommandHandler; 39 import org.drftpd.commands.CommandHandlerFactory; 40 import org.drftpd.commands.UnhandledCommandException; 41 42 import socks.server.Ident; 43 44 47 public class Login 48 implements CommandHandlerFactory, CommandHandler, Cloneable { 49 50 private static final Logger logger = Logger.getLogger(Login.class); 51 54 protected InetAddress _idntAddress; 55 protected String _idntIdent; 56 57 61 private FtpReply doIDNT(BaseFtpConnection conn) { 62 if (_idntAddress != null) { 63 logger.error("Multiple IDNT commands"); 64 return new FtpReply(530, "Multiple IDNT commands"); 65 } 66 if (!conn 67 .getConfig() 68 .getBouncerIps() 69 .contains(conn.getClientAddress())) { 70 logger.warn("IDNT from non-bnc"); 71 return FtpReply.RESPONSE_530_ACCESS_DENIED; 72 } 73 String arg = conn.getRequest().getArgument(); 74 int pos1 = arg.indexOf('@'); 75 if (pos1 == -1) 76 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 77 int pos2 = arg.indexOf(':', pos1 + 1); 78 if (pos2 == -1) 79 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 80 81 try { 82 _idntAddress = InetAddress.getByName(arg.substring(pos1 + 1, pos2)); 83 _idntIdent = arg.substring(0, pos1).toString(); 84 } catch (UnknownHostException e) { 85 logger.info("Invalid hostname passed to IDNT", e); 86 return new FtpReply(501, "IDNT FAILED: " + e.getMessage()); 89 } 90 return null; 92 } 93 94 101 private FtpReply doPASS(BaseFtpConnection conn) { 102 if (conn.getUserNull() == null) { 103 return FtpReply.RESPONSE_503_BAD_SEQUENCE_OF_COMMANDS; 104 } 105 106 FtpRequest request = conn.getRequest(); 107 108 String pass = request.hasArgument() ? request.getArgument() : ""; 110 111 if (conn.getUserNull().checkPassword(pass)) { 113 conn.getUserNull().login(); 114 conn.setAuthenticated(true); 115 conn.getConnectionManager().dispatchFtpEvent( 116 new UserEvent(conn.getUserNull(), "LOGIN")); 117 118 FtpReply response = 119 new FtpReply( 120 230, 121 conn.jprintf(Login.class.getName(), "pass.success")); 122 try { 123 Textoutput.addTextToResponse(response, "welcome"); 124 } catch (IOException e) { 125 logger.warn("Error reading welcome", e); 126 } 127 return response; 128 } else { 129 return new FtpReply( 130 530, 131 conn.jprintf(Login.class.getName(), "pass.fail")); 132 } 133 } 134 135 141 private FtpReply doQUIT(BaseFtpConnection conn) { 142 143 conn.stop(); 144 return new FtpReply( 145 221, 146 conn.jprintf(Login.class.getName(), "quit.success")); 147 } 148 149 158 private FtpReply doUSER(BaseFtpConnection conn) { 159 FtpRequest request = conn.getRequest(); 160 conn.setAuthenticated(false); 161 conn.setUser(null); 162 163 if (!request.hasArgument()) { 165 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 166 } 167 168 User newUser; 169 try { 170 newUser = 171 conn.getConnectionManager().getUserManager().getUserByName( 172 request.getArgument()); 173 } catch (NoSuchUserException ex) { 174 return new FtpReply(530, ex.getMessage()); 175 } catch (UserFileException ex) { 176 logger.warn("", ex); 177 return new FtpReply(530, "IOException: " + ex.getMessage()); 178 } catch (RuntimeException ex) { 179 logger.error("", ex); 180 return new FtpReply(530, ex.getMessage()); 181 } 182 183 if (newUser.isDeleted()) { 184 return FtpReply.RESPONSE_530_ACCESS_DENIED; 185 } 186 187 List masks = newUser.getIpMasks2(); 188 191 String ident = null; 192 for (Iterator iter = masks.iterator(); iter.hasNext();) { 193 HostMask mask = (HostMask) iter.next(); 194 if (_idntAddress == null 197 && ident == null 198 && mask.isIdentMaskSignificant()) { 199 Ident id = new Ident(conn.getControlSocket()); 200 if (id.successful) { 201 ident = id.userName; 202 if (ident.indexOf('@') != -1) { 203 return new FtpReply(530, "Invalid ident response"); 204 } 205 } else { 206 logger.warn( 207 "Failed to get ident response: " + id.errorMessage); 208 ident = ""; 209 } 210 } 211 212 if ((_idntAddress != null 213 && mask.matches(_idntIdent, _idntAddress)) 214 || (_idntAddress == null 215 && (mask.matches(ident, conn.getClientAddress())))) { 216 FtpReply response = 219 conn.getConnectionManager().canLogin(conn, newUser); 220 if (response != null) { 221 return response; 222 } 223 conn.setUser(newUser); 224 return new FtpReply( 225 331, 226 conn.jprintf(Login.class.getName(), "user.success")); 227 } 228 } 229 return FtpReply.RESPONSE_530_ACCESS_DENIED; 231 } 232 233 public FtpReply execute(BaseFtpConnection conn) 234 throws UnhandledCommandException { 235 String cmd = conn.getRequest().getCommand(); 236 if ("USER".equals(cmd)) 237 return doUSER(conn); 238 if ("PASS".equals(cmd)) 239 return doPASS(conn); 240 if ("QUIT".equals(cmd)) 241 return doQUIT(conn); 242 if ("IDNT".equals(cmd)) 243 return doIDNT(conn); 244 throw UnhandledCommandException.create(Login.class, conn.getRequest()); 245 } 246 247 public String [] getFeatReplies() { 248 return null; 249 } 250 251 public CommandHandler initialize( 252 BaseFtpConnection conn, 253 CommandManager initializer) { 254 try { 255 return (Login) clone(); 256 } catch (CloneNotSupportedException e) { 257 throw new RuntimeException (e); 258 } 259 } 260 261 public void load(CommandManagerFactory initializer) { 262 } 263 264 public void unload() { 265 } 266 } 267 | Popular Tags |