operator-starttls.cc (3460B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * operator-starttls.cc 5 * 6 * Description: 7 * Implementation of the STARTTLS command. 8 * 9 * Authors: 10 * Andreas Aardal Hanssen <andreas-binc curly bincimap spot org> 11 * 12 * Bugs: 13 * 14 * ChangeLog: 15 * 16 * -------------------------------------------------------------------- 17 * Copyright 2002-2005 Andreas Aardal Hanssen 18 * 19 * This program is free software; you can redistribute it and/or modify 20 * it under the terms of the GNU General Public License as published by 21 * the Free Software Foundation; either version 2 of the License, or 22 * (at your option) any later version. 23 * 24 * This program is distributed in the hope that it will be useful, 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 * GNU General Public License for more details. 28 * 29 * You should have received a copy of the GNU General Public License 30 * along with this program; if not, write to the Free Software 31 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. 32 * -------------------------------------------------------------------- 33 */ 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 #endif 37 38 #ifdef WITH_SSL 39 40 #include <string> 41 #include <iostream> 42 43 #include "recursivedescent.h" 44 #include "io.h" 45 #include "io-ssl.h" 46 #include "session.h" 47 #include "depot.h" 48 #include "operators.h" 49 50 using namespace ::std; 51 using namespace Binc; 52 53 //---------------------------------------------------------------------- 54 StarttlsOperator::StarttlsOperator(void) 55 { 56 } 57 58 //---------------------------------------------------------------------- 59 StarttlsOperator::~StarttlsOperator(void) 60 { 61 } 62 63 //---------------------------------------------------------------------- 64 const string StarttlsOperator::getName(void) const 65 { 66 return "STARTTLS"; 67 } 68 69 //---------------------------------------------------------------------- 70 int StarttlsOperator::getState(void) const 71 { 72 return Session::NONAUTHENTICATED 73 | Session::AUTHENTICATED 74 | Session::SELECTED; 75 } 76 77 //------------------------------------------------------------------------ 78 Operator::ProcessResult StarttlsOperator::process(Depot &depot, 79 Request &command) 80 { 81 Session &session = Session::getInstance(); 82 IO &com = IOFactory::getInstance().get(1); 83 84 if (session["sslmode"] != "") { 85 session.setLastError("Already in TLS mode"); 86 return BAD; 87 } 88 89 com << command.getTag() 90 << " OK STARTTLS completed, begin TLS negotiation now" << endl; 91 com.flushContent(); 92 com.flushOnEndl(); 93 94 SSLEnabledIO *sslcom; 95 sslcom = dynamic_cast<SSLEnabledIO *>(&IOFactory::getInstance().get(1)); 96 if (!sslcom) { 97 // Can this ever happen? 98 session.setLastError("An internal error occurred when" 99 " entering SSL mode. "); 100 return NO; 101 } else { 102 if (!sslcom->setModeSSL()) { 103 session.setLastError(sslcom->getLastError()); 104 return NO; 105 } 106 } 107 108 session.add("sslmode", "yes"); 109 110 return NOTHING; 111 } 112 113 //---------------------------------------------------------------------- 114 Operator::ParseResult StarttlsOperator::parse(Request &c_in) const 115 { 116 Session &session = Session::getInstance(); 117 118 if (c_in.getUidMode()) 119 return REJECT; 120 121 Operator::ParseResult res; 122 if ((res = expectCRLF()) != ACCEPT) { 123 session.setLastError("Expected CRLF"); 124 return ERROR; 125 } else 126 return ACCEPT; 127 } 128 129 #endif