001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.protocols.smtp.core.esmtp;
021
022 import java.util.ArrayList;
023 import java.util.Collection;
024 import java.util.List;
025
026 import org.apache.james.protocols.api.Request;
027 import org.apache.james.protocols.api.Response;
028 import org.apache.james.protocols.api.handler.CommandHandler;
029 import org.apache.james.protocols.smtp.SMTPResponse;
030 import org.apache.james.protocols.smtp.SMTPRetCode;
031 import org.apache.james.protocols.smtp.SMTPSession;
032 import org.apache.james.protocols.smtp.SMTPStartTLSResponse;
033 import org.apache.james.protocols.smtp.dsn.DSNStatus;
034
035 /**
036 * Handles STARTTLS command
037 */
038 public class StartTlsCmdHandler implements CommandHandler<SMTPSession>, EhloExtension {
039 /**
040 * The name of the command handled by the command handler
041 */
042 private final static String COMMAND_NAME = "STARTTLS";
043
044 /**
045 * @see org.apache.james.protocols.api.handler.CommandHandler#getImplCommands()
046 */
047 public Collection<String> getImplCommands() {
048 Collection<String> commands = new ArrayList<String>();
049 commands.add(COMMAND_NAME);
050 return commands;
051 }
052
053 /**
054 * Handler method called upon receipt of a STARTTLS command. Resets
055 * message-specific, but not authenticated user, state.
056 *
057 */
058 public Response onCommand(SMTPSession session, Request request) {
059 String command = request.getCommand();
060 String parameters = request.getArgument();
061 if (session.isStartTLSSupported()) {
062 if (session.isTLSStarted()) {
063 SMTPResponse response = new SMTPResponse("500", DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_CMD) + " TLS already active RFC2487 5.2");
064 return response;
065 } else {
066 SMTPResponse response;
067 if ((parameters == null) || (parameters.length() == 0)) {
068 response = new SMTPStartTLSResponse("220", DSNStatus.getStatus(DSNStatus.SUCCESS, DSNStatus.UNDEFINED_STATUS) + " Ready to start TLS");
069 } else {
070 response = new SMTPResponse("501 " + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_ARG) + " Syntax error (no parameters allowed) with STARTTLS command");
071 }
072 return response;
073
074 }
075
076 } else {
077 StringBuilder result = new StringBuilder();
078 result.append(DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_CMD)).append(" Command ").append(command).append(" unrecognized.");
079 SMTPResponse response = new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_COMMAND_UNRECOGNIZED, result);
080 return response;
081 }
082 }
083
084 /**
085 * @see org.apache.james.protocols.smtp.core.esmtp.EhloExtension#getImplementedEsmtpFeatures(org.apache.james.protocols.smtp.SMTPSession)
086 */
087 public List<String> getImplementedEsmtpFeatures(SMTPSession session) {
088 List<String> esmtpextensions = new ArrayList<String>();
089 // SMTP STARTTLS
090 if (!session.isTLSStarted() && session.isStartTLSSupported()) {
091 esmtpextensions.add("STARTTLS");
092 }
093 return esmtpextensions;
094
095 }
096
097 }