--- ./broker.cc 2023-08-22 22:04:58.667689000 +0200 +++ ../../bincimap-2.0.5/src//broker.cc 2023-08-14 12:04:05.654922000 +0200 @@ -105,7 +105,8 @@ //---------------------------------------------------------------------- Operator *Broker::get(const string &name) const { - if (operators.find(name) == operators.end()) return 0; + if (operators.find(name) == operators.end()) + return 0; return operators.find(name)->second; } --- ./imapserver.cc 2023-08-22 11:30:32.929436000 +0200 +++ ../../bincimap-2.0.5/src//imapserver.cc 2023-08-19 15:30:18.579113000 +0200 @@ -74,7 +74,6 @@ showGreeting(); } else { bincInfo << "<" << session.getEnv("USER") << "> logged in" << "\n"; - bincInfo << "<" << getenv("USER") << "> logged in" << "\n"; } bincInfo.flush(); --- ./iodevice.cc 2023-08-22 13:38:19.325053000 +0200 +++ ../../bincimap-2.0.5/src//iodevice.cc 2023-08-14 12:59:30.985424000 +0200 @@ -40,11 +40,13 @@ static std::ostream &(*endl_funcptr)(ostream &) = endl; - if (source != endl_funcptr) return *this; + if (source != endl_funcptr) + return *this; outputBuffer << "\r\n"; - if (dumpfd) ::write(dumpfd, "\r\n", 2); + if (dumpfd) + ::write(dumpfd, "\r\n", 2); if (flags & FlushesOnEndl) flush(); @@ -64,7 +66,8 @@ //------------------------------------------------------------------------ void IODevice::clear() { - if (!(flags & IsEnabled)) return; + if (!(flags & IsEnabled)) + return; inputBuffer.clear(); outputBuffer.clear(); @@ -73,20 +76,26 @@ //------------------------------------------------------------------------ bool IODevice::flush() { - if (!(flags & IsEnabled)) return true; + if (!(flags & IsEnabled)) + return true; WriteResult writeResult = WriteWait; do { unsigned int s = outputBuffer.getSize(); - if (s == 0) break; - if (!waitForWrite()) return false; + if (s == 0) + break; + + if (!waitForWrite()) + return false; + writeResult = write(); - if (writeResult == WriteError) return false; + if (writeResult == WriteError) + return false; + writeCount += s - outputBuffer.getSize(); } while (outputBuffer.getSize() > 0 && writeResult == WriteWait); outputBuffer.clear(); - return true; } @@ -159,11 +168,13 @@ bool IODevice::readStr(string *dest, unsigned int max) { // If max is 0, fill the input buffer once only if it's empty. - if (!max && inputBuffer.getSize() == 0 && !fillInputBuffer()) return false; + if (!max && inputBuffer.getSize() == 0 && !fillInputBuffer()) + return false; // If max is != 0, wait until we have max. while (max && inputBuffer.getSize() < max) { - if (!fillInputBuffer()) return false; + if (!fillInputBuffer()) + return false; } unsigned int bytesToRead = max ? max : inputBuffer.getSize(); @@ -174,21 +185,22 @@ inputBuffer.popString(bytesToRead); readCount += bytesToRead; - return true; } //------------------------------------------------------------------------ bool IODevice::readChar(char *dest) { - if (inputBuffer.getSize() == 0 && !fillInputBuffer()) return false; + if (inputBuffer.getSize() == 0 && !fillInputBuffer()) + return false; char c = inputBuffer.popChar(); - if (dest) *dest = c; - if (dumpfd) ::write(dumpfd, &c, 1); + if (dest) + *dest = c; + if (dumpfd) + ::write(dumpfd, &c, 1); ++readCount; - return true; } @@ -209,8 +221,10 @@ { char dest = '\0'; do { - if (!readChar(&dest)) return false; - if (dumpfd) ::write(dumpfd, &dest, 1); + if (!readChar(&dest)) + return false; + if (dumpfd) + ::write(dumpfd, &dest, 1); } while (c != dest); return true; @@ -278,6 +292,7 @@ << Session::getInstance().getIP() << "-XXXXXX"; char *safename = strdup(ss.str().c_str()); dumpfd = mkstemp(safename); - if (dumpfd == -1) dumpfd = 0; + if (dumpfd == -1) + dumpfd = 0; delete safename; } --- ./operator-authenticate.cc 2023-08-24 10:14:07.627463000 +0200 +++ ../../bincimap-2.0.5/src//operator-authenticate.cc 2023-08-19 15:24:29.665376000 +0200 @@ -166,11 +166,9 @@ putenv(strdup(("BINCIMAP_LOGIN=AUTHENTICATE+" + command.getTag()).c_str())); - // FEH: put the username in the environment for logging purpose + // put the username in the environment for logging purpose - // FIXME: - session.setEnv("USER", username.c_str()); -// putenv(strdup(("USER=" + username).c_str())); + session.setEnv("USER", username.c_str()); // the authenticate function calls a stub which does the actual // authentication. the function returns 0 (success), 1 (internal --- ./operator-capability.cc 2023-08-23 12:02:49.204839000 +0200 +++ ../../bincimap-2.0.5/src//operator-capability.cc 2023-08-21 15:12:32.915708000 +0200 @@ -12,7 +12,6 @@ #include "operators.h" #include "recursivedescent.h" #include "session.h" -#include "globals.h" using namespace ::std; using namespace Binc; @@ -37,8 +36,8 @@ int CapabilityOperator::getState() const { return Session::NONAUTHENTICATED - | Session::AUTHENTICATED - | Session::SELECTED; + | Session::AUTHENTICATED + | Session::SELECTED; } //---------------------------------------------------------------------- @@ -53,11 +52,10 @@ { Session &session = Session::getInstance(); - bincClient << "* CAPABILITY " << IMAP_VERSION ; + bincClient << "* CAPABILITY IMAP4rev1"; if (session.getState() == Session::NONAUTHENTICATED) { - if (getenv("UCSPITLS")) - if (!session.command.ssl) bincClient << " STARTTLS"; + if (!session.command.ssl) bincClient << " STARTTLS"; if (session.command.ssl || session.hasEnv("ALLOW_NONSSL_PLAINTEXT_LOGINS")) bincClient << " AUTH=LOGIN AUTH=PLAIN"; --- ./operator-fetch.cc 2023-08-22 11:28:00.260614000 +0200 +++ ../../bincimap-2.0.5/src//operator-fetch.cc 2023-08-21 11:54:04.569792000 +0200 @@ -372,9 +372,9 @@ pendingUpdates(mailbox, PendingUpdates::FLAGS - | PendingUpdates::EXISTS - | PendingUpdates::EXPUNGE - | PendingUpdates::RECENT, true); + | PendingUpdates::EXISTS + | PendingUpdates::EXPUNGE + | PendingUpdates::RECENT, true); return OK; } --- ./operator-id.cc 2023-08-23 22:43:11.362445000 +0200 +++ ../../bincimap-2.0.5/src//operator-id.cc 2023-08-21 15:05:56.720615000 +0200 @@ -1,8 +1,8 @@ /** -------------------------------------------------------------------- * @file operator-id.cc - * @brief Operator for the ID extension. Described in RFC2971 Oct 2000. + * @brief Operator for the ID command. * @author Erwin Hoffmann - * @date 22.09.2023 + * @date 2023 * ------------------------------------------------------------------ **/ #include #include @@ -46,10 +46,13 @@ Operator::ProcessResult IdOperator::process(Depot &depot, Request &command) { - bincClient << "* ID (\"name\" \"Binc IMAP\"" + Session &session = Session::getInstance(); + + bincClient << " * ID (\"name\" \"Binc IMAP\"" << " \"version\" \"" << BINC_VERSION "\")" << endl; + bincClient.flush(); - return NOTHING; + return OK; } //---------------------------------------------------------------------- @@ -57,15 +60,13 @@ { Session &session = Session::getInstance(); - if (c_in.getUidMode()) return REJECT; + if (c_in.getUidMode()) return ACCEPT; -/* FIXME: We are not interested in the parsing result Operator::ParseResult res; if ((res = expectSPACE()) != ACCEPT) { session.setLastError("Expected SPACE"); return res; } -*/ c_in.setName("ID"); --- ./operator-login.cc 2023-08-23 22:42:22.681346000 +0200 +++ ../../bincimap-2.0.5/src//operator-login.cc 2023-08-16 19:37:07.694225000 +0200 @@ -48,7 +48,7 @@ //------------------------------------------------------------------------ Operator::ProcessResult LoginOperator::process(Depot &depot, - Request &command) + Request &command) { Session &session = Session::getInstance(); @@ -79,7 +79,7 @@ break; case -1: bincClient << "* BYE The server died unexpectedly. Please contact " - "your system administrator for more information." << endl; + "your system administrator for more information." << endl; break; } --- ./operator-logout.cc 2023-08-22 13:41:49.546630000 +0200 +++ ../../bincimap-2.0.5/src//operator-logout.cc 2023-08-14 23:04:43.460303000 +0200 @@ -41,8 +41,8 @@ int LogoutOperator::getState() const { return Session::NONAUTHENTICATED - | Session::AUTHENTICATED - | Session::SELECTED; + | Session::AUTHENTICATED + | Session::SELECTED; } //------------------------------------------------------------------------ --- ./operator-noop.cc 2023-08-22 12:11:45.876657000 +0200 +++ ../../bincimap-2.0.5/src//operator-noop.cc 2023-08-15 15:05:11.493837000 +0200 @@ -51,7 +51,8 @@ { Session &session = Session::getInstance(); - if (c_in.getUidMode()) return REJECT; + if (c_in.getUidMode()) + return REJECT; Operator::ParseResult res; if ((res = expectCRLF()) != ACCEPT) { --- ./operator-starttls.cc 2023-08-24 10:24:53.204617000 +0200 +++ ../../bincimap-2.0.5/src//operator-starttls.cc 2023-08-19 16:15:15.622679000 +0200 @@ -1,13 +1,11 @@ /** -------------------------------------------------------------------- * @file operator-starttls.cc - * @brief Implementation of the STARTTLS command - based on sslserver - * @author Andreas Aardal Hanssen, Erwin Hoffmann - * @date 2002-2005, 2023 + * @brief Implementation of the STARTTLS command. + * @author Andreas Aardal Hanssen + * @date 2002-2005 * ----------------------------------------------------------------- **/ #include #include -#include -#include #include "recursivedescent.h" #include "iodevice.h" @@ -43,37 +41,6 @@ | Session::SELECTED; } -//---------------------------------------------------------------------- -int StarttlsOperator::goStartTLS () const -{ - Session &session = Session::getInstance(); - - if (getenv("UCSPITLS")) { - string fdstr; - int fd; - - fdstr = session.getEnv("SSLCTLFD"); - fd = std::stoi(fdstr); - if (write(fd,"Y",1) < 1) return NOTHING; - - fdstr = session.getEnv("SSLREADFD"); - fd = std::stoi(fdstr); - if (fcntl(fd,F_GETFL,0) == -1) return NOTHING; - close (0); - if (fcntl(fd,F_DUPFD,0) == -1) return NOTHING; - close (fd); - - fdstr = session.getEnv("SSLWRITEFD"); - fd = std::stoi(fdstr); - if (fcntl(fd,F_GETFL,0) == -1) return NOTHING; - close (1); - if (fcntl(fd,F_DUPFD,1) == -1) return NOTHING; - close (fd); - } - - return ACCEPT; -} - //------------------------------------------------------------------------ Operator::ProcessResult StarttlsOperator::process(Depot &depot, Request &command) @@ -84,13 +51,11 @@ return BAD; } - bincClient << "OK STARTTLS completed, begin TLS session now" << endl; + bincClient << command.getTag() + << " OK STARTTLS completed, begin TLS negotiation now" << endl; bincClient.flush(); - if (goStartTLS() == ACCEPT) - session.command.ssl = true; - else - return NO; + session.command.ssl = true; return NOTHING; } @@ -100,7 +65,8 @@ { Session &session = Session::getInstance(); - if (c_in.getUidMode()) return REJECT; + if (c_in.getUidMode()) + return REJECT; Operator::ParseResult res; if ((res = expectCRLF()) != ACCEPT) { --- ./session-initialize-bincimap-up.cc 2023-08-24 10:30:35.277625000 +0200 +++ ../../bincimap-2.0.5/src//session-initialize-bincimap-up.cc 2023-08-21 15:50:02.457680000 +0200 @@ -1,8 +1,8 @@ /** -------------------------------------------------------------------- * @file session-initialize-bincimap-up.cc - * @brief bincimap-up requires sslserver - * @author Andreas Aardal Hanssen, Erwin Hoffmann - * @date 2002-2005, 2023 + * @brief <---> + * @author Andreas Aardal Hanssen + * @date 2002-2005 * ----------------------------------------------------------------- **/ #include #include @@ -27,6 +27,15 @@ extern char **environ; +namespace { + //------------------------------------------------------------------------ + void usage(char *name) + { + printf("Please refer to the man pages for bincimap-up and bincimap\n"); + printf("for more information about how to invoke Binc IMAP.\n"); + } +} + //---------------------------------------------------------------------- bool Session::initialize(int argc, char *argv[]) { @@ -51,26 +60,38 @@ return false; } - // Show version if asked for it + // imaps (port 993) -- requires sslserver with option -e + + int stls = 0; + string tlssession = session.getEnv("SSL_SESSION_ID"); + trim(tlssession); + if (tlssession.size() > 2) { + session.command.ssl = true; + stls = -1 ; + } + + // Show help if asked for it if (session.command.version) { - printf("Binc IMAP v" BINC_VERSION"\n"); + printf("Binc IMAP v" BINC_VERSION IMAP_VERSION "\n"); return false; } // Let the command line args override the global settings. session.assignCommandLineArgs(); - // for log input + + // log settings string ip = getenv("TCP6REMOTEIP") ? getenv("TCP6REMOTEIP") : getenv("TCPREMOTEIP") ? getenv("TCPREMOTEIP") : "?"; session.setIP(ip); + string logtype = session.getEnv("LOG_TYPE"); lowercase(logtype); trim(logtype); if (logtype == "multilog" || logtype == "stderr") { - MultilogDevice *device = new MultilogDevice(IODevice::IsEnabled - | IODevice::FlushesOnEndl); + MultilogDevice *device = new MultilogDevice(IODevice::IsEnabled + | IODevice::FlushesOnEndl); ioFactory.addDevice(device); } else if (logtype == "" || logtype == "syslog") { const string f = session.getEnv("SYSLOG_FACILITY"); @@ -89,13 +110,13 @@ else if (f == "LOG_LOCAL6") facility = LOG_LOCAL6; else if (f == "LOG_LOCAL7") facility = LOG_LOCAL7; else facility = LOG_DAEMON; - } + } SyslogDevice *device = new SyslogDevice(IODevice::IsEnabled - | IODevice::FlushesOnEndl, - "bincimap-up", - LOG_NDELAY | LOG_PID, - facility); + | IODevice::FlushesOnEndl, + "bincimap-up", + LOG_NDELAY | LOG_PID, + facility); ioFactory.addDevice(device); } @@ -105,25 +126,34 @@ MultilogDevice *device = new MultilogDevice(IODevice::IsEnabled - | IODevice::FlushesOnEndl); - ioFactory.addDevice(device); + | IODevice::FlushesOnEndl); + ioFactory.addDevice(device); // Now that we know the log type, we can flush. IOFactory::getLogger().setFlags(IODevice::FlushesOnEndl); IOFactory::getLogger().setOutputLevelLimit(IODevice::InfoLevel); - // imaps (port 993) -- requires sslserver with option -e + string ucspitls = session.getEnv("UCSPITLS"); + if (ucspitls == "+") stls = 1; + if (ucspitls == "-") stls = 0; + if (ucspitls == "!") stls = 2; - int stls = 0; - if (getenv("SSL_SESSION_ID")) { - session.command.ssl = true; - stls = -1; - // else we will do starttls - requires new FDs - } else if (getenv("UCSPITLS")) { - string ucspitls = session.getEnv("UCSPITLS"); - if (ucspitls == "+") stls = 1; - if (ucspitls == "-") stls = 0; - if (ucspitls == "!") stls = 2; + if (stls > 0) { + string fdstr; + int fd; + fdstr = session.getEnv("SSLCTLFD"); + fd = std::stoi(fdstr); + if (write(fd,"Y",1) < 1) return 0; + + fdstr = session.getEnv("SSLREADFD"); + fd = std::stoi(fdstr); + if (dup2(0,fd) == -1) return 0; + close(fd); + + fdstr = session.getEnv("SSLWRITEFD"); + fd = std::stoi(fdstr); + if (dup2(1,fd) == -1) return 0; + close(fd); } BrokerFactory &brokerfactory = BrokerFactory::getInstance(); @@ -133,8 +163,7 @@ brokerfactory.assign("LOGIN", new LoginOperator()); brokerfactory.assign("LOGOUT", new LogoutOperator()); brokerfactory.assign("NOOP", new NoopOperator()); - brokerfactory.assign("ID", new IdOperator()); - if (stls > 0) brokerfactory.assign("STARTTLS", new StarttlsOperator()); + if (stls) brokerfactory.assign("STARTTLS", new StarttlsOperator()); bincClient.setTimeout(60); --- ./session-initialize-bincimapd.cc 2023-08-24 10:37:13.007980000 +0200 +++ ../../bincimap-2.0.5/src//session-initialize-bincimapd.cc 2023-08-21 12:20:24.757336000 +0200 @@ -1,9 +1,12 @@ -/** -------------------------------------------------------------------- - * @file session-initialize-bincimapd.cc - * @brief <---> - * @author Andreas Aardal Hanssen, Erwin Hoffmann - * @date 2002-2005, 2023 +/* -------------------------------------------------------------------- + * Filename: + * session-initialize-bincimap-up.cc + * + * Description: + * <---> * -------------------------------------------------------------------- + * Copyright 2002-2005 Andreas Aardal Hanssen + * -------------------------------------------------------------------- */ #include #include @@ -21,7 +24,6 @@ #include "syslogdevice.h" #include "tools.h" #include "convert.h" - #include #include #include @@ -31,14 +33,24 @@ extern char **environ; +namespace { + //------------------------------------------------------------------------ + void usage(char *name) + { + bincInfo << "Please refer to the man pages for bincimap-up and bincimapd" + << endl; + bincInfo << "for more information about how to invoke Binc IMAP." << endl; + bincInfo.flush(); + } +} + //---------------------------------------------------------------------- bool Session::initialize(int argc, char *argv[]) { IOFactory &ioFactory = IOFactory::getInstance(); - IODevice *stdioDevice = new StdIODevice(IODevice::IsEnabled - | IODevice::HasInputLimit - | IODevice::HasTimeout); + | IODevice::HasInputLimit + | IODevice::HasTimeout); stdioDevice->setFlags(IODevice::HasOutputLimit); stdioDevice->setMaxOutputBufferSize(TRANSFER_BUFFER_SIZE); ioFactory.addDevice(stdioDevice); @@ -55,9 +67,9 @@ return false; } - // Show version if asked for it + // Show help if asked for it if (session.command.version) { - printf("Binc IMAP v" BINC_VERSION"\n"); + printf("Binc IMAP v" BINC_VERSION IMAP_VERSION"\n"); return false; } @@ -96,15 +108,27 @@ session.setEnv("SYSLOG_FACILITY", toString(facility)); ioFactory.addDevice(new SyslogDevice(IODevice::IsEnabled, - "bincimapd", - LOG_NDELAY | LOG_PID, - facility)); + "bincimapd", + LOG_NDELAY | LOG_PID, + facility)); } // Now that we know the log type, we can flush. IOFactory::getLogger().flush(); IOFactory::getLogger().setFlags(IODevice::FlushesOnEndl); IOFactory::getLogger().setOutputLevelLimit(IODevice::InfoLevel); + + // Show help if asked for it + if (session.command.help) { + usage(argv[0]); + return false; + } + + // Show help if asked for it + if (session.command.version) { + bincInfo << "Binc IMAP v" << BINC_VERSION IMAP_VERSION << endl; + return false; + } char *logindetails = getenv("BINCIMAP_LOGIN"); if (logindetails == 0) { --- ./session.cc 2023-08-22 11:08:44.043704000 +0200 +++ ../../bincimap-2.0.5/src//session.cc 2023-08-20 13:56:00.410613000 +0200 @@ -137,7 +137,7 @@ { args.addOptional("h|?|help", "Display this help screen", true); args.addOptional("version", "Display the version of Binc IMAP", true); - args.addOptional("a|allow-plain", "Allow authentication when not TLS protected", true); + args.addOptional("a|allow-plain", "Allow authentication when not in SSL", true); args.addOptional("v|show-version", "Enable verbose IMAP greeting", false); args.addOptional("l|log-type", "Sets the method used for logging", false); args.addOptional("d|depot", "Sets the depot type", false); --- ./syslogdevice.cc 2023-08-22 13:36:36.473713000 +0200 +++ ../../bincimap-2.0.5/src//syslogdevice.cc 2023-08-19 21:24:47.622270000 +0200 @@ -62,7 +62,8 @@ out += *i; } - if (out != "") syslog(priority, out.c_str(), out.size()); + if (out != "") + syslog(priority, out.c_str(), out.size()); outputBuffer.clear(); return WriteDone;