1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.pop3server;
23
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.james.services.MailRepository;
26 import org.apache.james.util.POP3BeforeSMTPHelper;
27
28 /**
29 * Handles PASS command
30 */
31 public class PassCmdHandler extends AbstractLogEnabled implements CommandHandler {
32
33 /**
34 * @see org.apache.james.pop3server.CommandHandler#onCommand(POP3Session)
35 */
36 public void onCommand(POP3Session session) {
37 doPASS(session,session.getCommandArgument());
38 }
39
40 /**
41 * Handler method called upon receipt of a PASS command.
42 * Reads in and validates the password.
43 *
44 * @param argument the first argument parsed by the parseCommand method
45 */
46 private void doPASS(POP3Session session,String argument) {
47 String responseString = null;
48 if (session.getHandlerState() == POP3Handler.AUTHENTICATION_USERSET && argument != null) {
49 String passArg = argument;
50 if (session.getConfigurationData().getUsersRepository().test(session.getUser(), passArg)) {
51 try {
52 MailRepository inbox = session.getConfigurationData().getMailServer().getUserInbox(session.getUser());
53 if (inbox == null) {
54 throw new IllegalStateException("MailServer returned a null inbox for "+session.getUser());
55 }
56 session.setUserInbox(inbox);
57 RsetCmdHandler.stat(session, getLogger());
58
59 // Store the ipAddress to use it later for pop before smtp
60 POP3BeforeSMTPHelper.addIPAddress(session.getRemoteIPAddress());
61
62 StringBuffer responseBuffer =
63 new StringBuffer(64)
64 .append(POP3Handler.OK_RESPONSE)
65 .append(" Welcome ")
66 .append(session.getUser());
67 responseString = responseBuffer.toString();
68 session.setHandlerState(POP3Handler.TRANSACTION);
69 session.writeResponse(responseString);
70 } catch (RuntimeException e) {
71 getLogger().error("Unexpected error accessing mailbox for "+session.getUser(),e);
72 responseString = POP3Handler.ERR_RESPONSE + " Unexpected error accessing mailbox";
73 session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
74 session.writeResponse(responseString);
75 }
76 } else {
77 responseString = POP3Handler.ERR_RESPONSE + " Authentication failed.";
78 session.setHandlerState(POP3Handler.AUTHENTICATION_READY);
79 session.writeResponse(responseString);
80 }
81 } else {
82 responseString = POP3Handler.ERR_RESPONSE;
83 session.writeResponse(responseString);
84 }
85 }
86
87
88 }