session-initialize-bincimap-up.cc (7067B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * session-initialize-bincimap-up.cc 5 * 6 * Description: 7 * <---> 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 #include <syslog.h> 39 40 #include "broker.h" 41 #include "io-ssl.h" 42 #include "io.h" 43 #include "session.h" 44 #include "storage.h" 45 #include "tools.h" 46 #include "convert.h" 47 #include <string> 48 #include <map> 49 50 using namespace ::std; 51 using namespace Binc; 52 53 extern char **environ; 54 55 namespace { 56 //------------------------------------------------------------------------ 57 void usage(char *name) 58 { 59 IO &logger = IOFactory::getInstance().get(2); 60 61 logger << "Please refer to the man pages for bincimap-up and bincimapd" 62 << endl; 63 logger << "for more information about how to invoke Binc IMAP." << endl; 64 logger.flushContent(); 65 } 66 } 67 68 //---------------------------------------------------------------------- 69 bool Session::initialize(int argc, char *argv[]) 70 { 71 IOFactory &iofactory = IOFactory::getInstance(); 72 73 #ifdef WITH_SSL 74 SSLEnabledIO *sslio = new SSLEnabledIO(stdout); 75 iofactory.assign(1, sslio); 76 #else 77 iofactory.assign(1, new IO(stdout)); 78 #endif 79 80 iofactory.assign(2, new IO(stderr)); 81 82 IO &com = iofactory.get(1); 83 IO &logger = iofactory.get(2); 84 85 Session &session = Session::getInstance(); 86 87 logger.noFlushOnEndl(); 88 89 // Read command line arguments 90 if (!session.parseRequestLine(argc, argv)) 91 return false; 92 93 // Show help if asked for it 94 if (session.command.help) { 95 usage(argv[0]); 96 logger.flushContent(); 97 return false; 98 } 99 100 // Show help if asked for it 101 if (session.command.version) { 102 logger << "Binc IMAP v" << VERSION << endl; 103 logger.flushContent(); 104 return false; 105 } 106 107 // Grab config file name 108 string configfile = session.command.configfile; 109 110 // Read configuration 111 if (configfile != "") { 112 113 // try to read global settings. 114 Storage gconfig(configfile, Storage::ReadOnly); 115 string section, key, value; 116 while (gconfig.get(§ion, &key, &value)) 117 session.globalconfig[section][key] = value; 118 119 if (!gconfig.eof()) { 120 logger << "error reading Binc IMAP's config file " 121 << configfile << ". Default values will be used instead: " 122 << gconfig.getLastError() << endl; 123 // don't flush! 124 } 125 } 126 127 // Read configuration settings 128 session.initConfig(); 129 130 // Let the command line args override the global settings. 131 session.assignCommandLineArgs(); 132 133 // log settings 134 string ipenv = session.globalconfig["Log"]["ip environment variable"]; 135 // Initialize logger 136 string ip = getenv(ipenv.c_str()) ? getenv(ipenv.c_str()) : 137 getenv("TCPREMOTEIP") ? getenv("TCPREMOTEIP") : 138 getenv("REMOTE_HOST") ? getenv("REMOTE_HOST") : 139 getenv("REMOTEIP") ? getenv("REMOTEIP") : 140 getenv("SSLREMOTEIP") ? getenv("SSLREMOTEIP") : "?"; 141 session.setIP(ip); 142 143 if (session.globalconfig["Log"]["type"] == "multilog" 144 || session.globalconfig["Log"]["type"] == "stderr") { 145 logger.setLogPrefix("unknown@" + ip + ":"); 146 logger.enableLogPrefix(); 147 } else if (session.globalconfig["Log"]["type"] == "" 148 || session.globalconfig["Log"]["type"] == "syslog") { 149 const string f = session.globalconfig["Log"]["syslog facility"]; 150 const string fn = session.globalconfig["Log"]["syslog facility number"]; 151 152 int facility; 153 154 if (fn != "") facility = atoi(fn); 155 else { 156 if (f == "LOG_USER") facility = LOG_USER; 157 else if (f == "LOG_LOCAL0") facility = LOG_LOCAL0; 158 else if (f == "LOG_LOCAL1") facility = LOG_LOCAL1; 159 else if (f == "LOG_LOCAL2") facility = LOG_LOCAL2; 160 else if (f == "LOG_LOCAL3") facility = LOG_LOCAL3; 161 else if (f == "LOG_LOCAL4") facility = LOG_LOCAL4; 162 else if (f == "LOG_LOCAL5") facility = LOG_LOCAL5; 163 else if (f == "LOG_LOCAL6") facility = LOG_LOCAL6; 164 else if (f == "LOG_LOCAL7") facility = LOG_LOCAL7; 165 else facility = LOG_DAEMON; 166 } 167 168 session.globalconfig["Log"]["syslog facility number"] = toString(facility); 169 170 logger.setModeSyslog("bincimap-up", facility); 171 } 172 173 // Now that we know the log type, we can flush. 174 logger.flushContent(); 175 logger.flushOnEndl(); 176 177 BrokerFactory &brokerfactory = BrokerFactory::getInstance(); 178 179 brokerfactory.assign("AUTHENTICATE", new AuthenticateOperator()); 180 brokerfactory.assign("CAPABILITY", new CapabilityOperator()); 181 brokerfactory.assign("LOGIN", new LoginOperator()); 182 brokerfactory.assign("LOGOUT", new LogoutOperator()); 183 brokerfactory.assign("NOOP", new NoopOperator()); 184 #ifdef WITH_SSL 185 brokerfactory.assign("STARTTLS", new StarttlsOperator()); 186 #endif 187 188 #ifdef WITH_SSL 189 // Set SSL mode if --ssl is passed 190 if (session.command.ssl) 191 if (sslio->setModeSSL()) { 192 session.add("sslmode", "yes"); 193 } else { 194 session.setLastError("SSL negotiation failed: " 195 + sslio->getLastError()); 196 return false; 197 } 198 #endif 199 200 session.setState(Session::NONAUTHENTICATED); 201 202 // Read timeout settings from global config 203 idletimeout = atoi(session.globalconfig["Session"]["idle timeout"]); 204 authtimeout = atoi(session.globalconfig["Session"]["auth timeout"]); 205 transfertimeout = atoi(session.globalconfig["Session"]["transfer timeout"]); 206 207 // Settings are not allowed to break the IMAP protocol 208 if (idletimeout < (30 * 60)) idletimeout = 30 * 60; 209 210 // No auth timeout is not allowed. 211 if (authtimeout < 30) authtimeout = 30; 212 213 // Set transfer timeout 214 com.setTransferTimeout(transfertimeout); 215 logger.setTransferTimeout(transfertimeout); 216 217 // Read transfer buffer size 218 int buffersize = atoi(session.globalconfig["Session"]["transfer buffer size"]); 219 com.setBufferSize(buffersize >= 0 ? buffersize : 0); 220 221 // umask settings 222 string umsk = session.globalconfig["Mailbox"]["umask"]; 223 if (umsk != "") { 224 unsigned int mode; 225 sscanf(session.globalconfig["Mailbox"]["umask"].c_str(), "%o", &mode); 226 umask((mode_t) mode); 227 } 228 229 // If the depot was not initialized properly, return false. 230 return true; 231 }