configuration.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // configuration.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gsmtp.h"
23 #include "gstr.h"
24 #include "configuration.h"
25 #include "commandline.h"
26 #include "gmessagestore.h"
27 #include "gpopsecrets.h"
28 #include "gstrings.h"
29 #include "gdebug.h"
30 
32  m_cl(cl)
33 {
34 }
35 
37 {
38  return
39  contains( "log" ) ||
40  contains( "as-client" ) ||
41  contains( "as-proxy" ) ||
42  contains( "as-server" ) ;
43 }
44 
46 {
47  return contains( "verbose" ) ;
48 }
49 
51 {
52  return contains( "debug" ) ;
53 }
54 
56 {
57  bool basic = !contains("no-syslog") && !contains("as-client") ;
58  bool override = contains( "syslog" ) ;
59  return override || basic ;
60 }
61 
63 {
64  return contains( "log-time" ) ;
65 }
66 
67 std::string Main::Configuration::logFile() const
68 {
69  return contains("log-file") ? value("log-file") : std::string() ;
70 }
71 
72 unsigned int Main::Configuration::port() const
73 {
74  return value( "port" , 25U ) ;
75 }
76 
77 G::Strings Main::Configuration::listeningInterfaces( const std::string & protocol ) const
78 {
79  // allow eg. "127.0.0.1,smtp=192.168.1.1,admin=10.0.0.1"
80 
81  // prepare the naive result by splitting the user's string by comma or slash separators
82  G::Strings result = m_cl.value( "interface" , ",/" ) ;
83 
84  // then weed out the ones that have an explicit protocol name that doesnt match the
85  // required protocol, removing the "protocol=" prefix at the same time to leave just
86  // the required list of addresses
87  for( G::Strings::iterator p = result.begin() ; p != result.end() ; )
88  {
89  if( protocol.empty() || protocol == G::Str::head( *p , (*p).find('=') , protocol ) )
90  *p++ = G::Str::tail( *p , (*p).find('=') , *p ) ;
91  else
92  p = result.erase( p ) ;
93  }
94 
95  return result ;
96 }
97 
99 {
100  G::Strings result = m_cl.value( "interface" , ",/" ) ;
101  // look for first "client=" value
102  {
103  for( G::Strings::iterator p = result.begin() ; p != result.end() ; ++p )
104  {
105  if( (*p).find("client=") == 0U )
106  return G::Str::tail( *p , (*p).find('=') , *p ) ;
107  }
108  }
109  // or use the first unqalified value
110  {
111  for( G::Strings::iterator p = result.begin() ; p != result.end() ; ++p )
112  {
113  if( (*p).find('=') == std::string::npos )
114  return *p ;
115  }
116  }
117  return std::string() ;
118 }
119 
120 unsigned int Main::Configuration::adminPort() const
121 {
122  return value( "admin" , 0U ) ;
123 }
124 
126 {
127  return
128  contains("close-stderr") ||
129  contains("as-proxy") ||
130  contains("as-server") ;
131 }
132 
134 {
135  return !contains("no-daemon") && !contains("as-client") ;
136 }
137 
139 {
140  return contains("spool-dir") ?
141  G::Path(value("spool-dir")) :
143 }
144 
146 {
147  const char * key = "forward-to" ;
148  if( contains("as-client") )
149  key = "as-client" ;
150  else if( contains("as-proxy") )
151  key = "as-proxy" ;
152  return contains(key) ? value(key) : std::string() ;
153 }
154 
156 {
157  return contains("forward") || contains("as-client") ;
158 }
159 
161 {
162  return !contains("dont-serve") && !contains("as-client") ;
163 }
164 
166 {
167  return contains("immediate") ;
168 }
169 
171 {
172  return contains("poll") && pollingTimeout() > 0U ;
173 }
174 
176 {
177  return value( "poll" , 0U ) ;
178 }
179 
181 {
182  // dont log if polling very frequently
183  return doPolling() && pollingTimeout() > 60U ;
184 }
185 
187 {
188  // this is not settable for now -- the 103 exit
189  // code has a similar effect -- see also comments below
190  return false ;
191 }
192 
194 {
195  // TODO -- it not completely logical to tie forwarding-on-disconnect
196  // in with polling, but it avoids the situation where messages can
197  // get missed if a polling run is already in progress when a
198  // forwarding-on-disconnect event occurs -- the scan of the spool
199  // directory is done at the start of the polling run -- the fix
200  // could be to allow clients to run in parallel in the Run class,
201  // or have a method to force the client to rescan the directory
202  // before finishing
203 
204  return
205  ( contains("poll") && pollingTimeout() == 0U ) ||
206  contains("as-proxy") ;
207 }
208 
210 {
211  return value( "prompt-timeout" , 20U ) ;
212 }
213 
215 {
216  return ! contains( "no-smtp" ) ;
217 }
218 
220 {
221  return contains( "pop" ) ;
222 }
223 
225 {
226  return contains( "pop-by-name" ) ;
227 }
228 
230 {
231  return contains( "pop-no-delete" ) ;
232 }
233 
234 unsigned int Main::Configuration::popPort() const
235 {
236  return value( "pop-port" , 110U ) ;
237 }
238 
240 {
241  return contains( "remote-clients" ) ;
242 }
243 
245 {
246  return contains( "admin" ) ;
247 }
248 
250 {
251  return contains( "pid-file" ) ;
252 }
253 
254 std::string Main::Configuration::pidFile() const
255 {
256  return value( "pid-file" ) ;
257 }
258 
260 {
261  return contains( "filter" ) ;
262 }
263 
264 std::string Main::Configuration::filter() const
265 {
266  return contains("filter") ? value("filter") : std::string() ;
267 }
268 
270 {
271  return contains("client-filter") ? value("client-filter") : std::string() ;
272 }
273 
274 unsigned int Main::Configuration::icon() const
275 {
276  return 0U ; // no longer used
277 }
278 
280 {
281  return contains( "hidden" ) ;
282 }
283 
285 {
286  return contains("client-auth") ? value("client-auth") : std::string() ;
287 }
288 
290 {
291  return contains( "client-tls" ) ;
292 }
293 
295 {
296  return contains( "client-tls-connection" ) ;
297 }
298 
299 unsigned int Main::Configuration::tlsConfig() const
300 {
301  return value( "tls-config" , 0U ) ;
302 }
303 
305 {
306  return contains("server-tls") ? value("server-tls") : std::string() ;
307 }
308 
310 {
311  return contains("pop-auth") ? value("pop-auth") : GPop::Secrets::defaultPath() ;
312 }
313 
315 {
316  return contains("server-auth") ? value("server-auth") : std::string() ;
317 }
318 
320 {
321  return value( "response-timeout" , 30U * 60U ) ;
322 }
323 
325 {
326  return value( "connection-timeout" , 40U ) ;
327 }
328 
330 {
331  return connectionTimeout() ;
332 }
333 
334 std::string Main::Configuration::fqdn( std::string default_ ) const
335 {
336  return contains("domain") ? value("domain") : default_ ;
337 }
338 
339 std::string Main::Configuration::nobody() const
340 {
341  return contains("user") ? value("user") : std::string("daemon") ;
342 }
343 
344 std::string Main::Configuration::verifier() const
345 {
346  return contains("verifier") ? value("verifier") : std::string() ;
347 }
348 
350 {
351  return contains( "admin-terminate" ) ;
352 }
353 
354 unsigned int Main::Configuration::maxSize() const
355 {
356  return value( "size" , 0U ) ;
357 }
358 
360 {
361  return contains("scanner") ? value("scanner") : std::string() ;
362 }
363 
365 {
366  return 10U ; // for now
367 }
368 
370 {
371  return 90U ; // for now
372 }
373 
375 {
376  return contains( "anonymous" ) ;
377 }
378 
380 {
381  return value( "filter-timeout" , 5U * 60U ) ;
382 }
383 
385 {
386  return contains( "peer-lookup" ) ;
387 }
388 
389 bool Main::Configuration::contains( const char * s ) const
390 {
391  return m_cl.contains( s ) ;
392 }
393 
394 std::string Main::Configuration::value( const char * s ) const
395 {
396  return m_cl.value( s ) ;
397 }
398 
399 unsigned int Main::Configuration::value( const char * s , unsigned int d ) const
400 {
401  return m_cl.value( s , d ) ;
402 }
403 
bool peerLookup() const
Returns true if there should be some attempt to look up the userid of SMTP peers connected from the t...
bool doSmtp() const
Returns true if listening for smtp connections.
bool immediate() const
Returns true if forwarding should occur as soon as each message body is received and before receipt i...
bool clientOverTls() const
Returns true if using the SMTP over TLS (vs.
std::string verifier() const
Returns the path of an external address verifier program.
std::string serverAddress() const
Returns the downstream server's address string.
bool pollingLog() const
Returns true if polling activity should be logged.
std::string filter() const
Returns the path to a server-side pre-processor.
bool doServing() const
Returns true if running as a server (SMTP, POP, admin or COM).
bool daemon() const
Returns true if running as a daemon.
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
bool anonymous() const
Returns true if the server protocol should be slightly more anonymous.
bool popNoDelete() const
Returns true if pop deletion is to be disabled.
unsigned int popPort() const
Returns the pop port number.
bool allowRemoteClients() const
Returns true if allowing remote clients to connect.
bool closeStderr() const
Returns true if stderr should be closed.
G::Path spoolDir() const
Returns the spool directory.
bool doAdmin() const
Returns true if listening for admin connections.
G::Strings listeningInterfaces(const std::string &protocol=std::string()) const
Returns the listening interface(s).
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:842
bool useSyslog() const
Returns true if generating syslog events.
bool forwardingOnStore() const
Returns true if forwarding should occur as each message is stored, after it is acknowledged.
unsigned int secureConnectionTimeout() const
Returns the timeout for establishing a secure connection.
bool verbose() const
Returns true if doing verbose logging.
std::string serverTlsFile() const
Returns the tls certificate file if the server should support tls.
bool debug() const
Returns true if doing debug-level logging.
unsigned int scannerConnectionTimeout() const
Returns a timeout for connecting to the scanner process.
std::string serverSecretsFile() const
Returns the server-side autentication secrets (password) file.
unsigned int promptTimeout() const
Returns the timeout for getting a prompt from the SMTP server.
static std::string head(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:834
std::string nobody() const
Returns the name of an unprivileged user.
A class which deals with the command-line interface to the process, both input and output...
Definition: commandline.h:44
bool log() const
Returns true if doing logging.
unsigned int connectionTimeout() const
Returns the client-side connection timeout value.
static G::Path defaultDirectory()
Returns a default spool directory, such as "/usr/local/var/spool/emailrelay".
std::string fqdn(std::string default_=std::string()) const
Returns the fully-qualified-domain-name override.
unsigned int pollingTimeout() const
Returns the timeout for periodic polling.
std::string logFile() const
Returns the path of a stderr replacement for logging.
unsigned int maxSize() const
Returns the maximum size of submitted messages, or zero.
std::string clientFilter() const
Returns the path to a client-side pre-processor.
std::string pidFile() const
Returns the pid file's path.
bool useFilter() const
Returns true if pre-processing.
Configuration(const CommandLine &cl)
Constructor. The reference is kept.
unsigned int icon() const
Returns the icon selector (win32).
bool withTerminate() const
Returns true if the admin interface should support the terminate command.
bool logTimestamp() const
Returns true if logging output should be timestamped.
std::string clientInterface() const
Returns the sending interface.
bool popByName() const
Returns true if the pop spool directory is modified according to the client name. ...
unsigned int port() const
Returns the main listening port number.
bool forwardingOnDisconnect() const
Returns true if forwarding should occur when the submitter's network connection disconnects.
bool doForwardingOnStartup() const
Returns true if running as a client.
unsigned int adminPort() const
Returns the admin port number.
bool hidden() const
Returns true if the main window is hidden (win32).
unsigned int tlsConfig() const
Returns TLS configuration flags.
std::string scannerAddress() const
Returns the address of a scanner process.
std::string clientSecretsFile() const
Returns the client-side autentication secrets (password) file.
bool usePidFile() const
Returns true if writing a pid file.
static std::string defaultPath()
Returns the default path.
A Path object represents a file system path.
Definition: gpath.h:44
bool doPolling() const
Returns true if doing poll-based forwarding.
bool doPop() const
Returns true if listening for pop connections.
unsigned int scannerResponseTimeout() const
Returns a timeout for talking to the scanner process.
unsigned int responseTimeout() const
Returns the client-side protocol timeout value.
unsigned int filterTimeout() const
Returns the timeout for executing an ansynchronous filter() or clientFilter() program.
bool clientTls() const
Returns true if the client protocol should take account of the server's tls capability.
std::string popSecretsFile() const
Returns the pop-server autentication secrets (password) file.