From cd3095e43b06e4a623cfc63cd990a484d6422191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ft?= Date: Sat, 16 May 2020 04:58:40 +0200 Subject: [PATCH] feat(core): Support smtps and STARTTLS for SMTP Support uri schemes for SMTP, enabling STARTTLS and SMTPS for SMTP connections. The new format for configuration value SMTPServer supports passing a URI scheme (either smtp:// or smtps://). To support old configurations, if no scheme is given, smtp:// is assumed. Fixes #31 --- Documentation/SOGoInstallationGuide.asciidoc | 10 ++++-- Scripts/sogo.conf | 2 +- SoObjects/SOGo/SOGoDefaults.plist | 2 +- SoObjects/SOGo/SOGoDomainDefaults.m | 10 +++++- SoObjects/SOGo/SOGoMailer.m | 34 +++++++------------- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Documentation/SOGoInstallationGuide.asciidoc b/Documentation/SOGoInstallationGuide.asciidoc index 3104279c0..4e6bc3511 100644 --- a/Documentation/SOGoInstallationGuide.asciidoc +++ b/Documentation/SOGoInstallationGuide.asciidoc @@ -1642,10 +1642,16 @@ are: |D |SOGoSMTPServer |The DNS name or IP address of the SMTP server used when _SOGoMailingMechanism_ is set to `smtp`. +Supported formats are: `smtp://domain:port`, `smtps://domain`, +`domain:port`, `smtp://domain:port/?tls=YES`. Using the option +`tls=YES` will enforce using STARTTLS smtp connections. Thus, +`smtp://localhost:587/?tls=YES` would use the default MUA port +on localhost with STARTTLS enforced. |D |SOGoSMTPAuthenticationType |Activate SMTP authentication and specifies which type is in use. -Current, only `PLAIN` is supported and other values will be ignored. +Current, only `PLAIN` is supported and other values will cause +the authentication to fail. |S |WOSendMail |The path of the sendmail binary. @@ -2214,7 +2220,7 @@ like this: SOGoTrashFolderName = Trash; SOGoJunkFolderName = Junk; SOGoMailingMechanism = smtp; - SOGoSMTPServer = 127.0.0.1; + SOGoSMTPServer = "smtp://127.0.0.1"; SOGoUserSources = ( { type = ldap; diff --git a/Scripts/sogo.conf b/Scripts/sogo.conf index 28adef971..462e615cf 100644 --- a/Scripts/sogo.conf +++ b/Scripts/sogo.conf @@ -26,7 +26,7 @@ //SOGoTrashFolderName = Trash; //SOGoIMAPServer = localhost; //SOGoSieveServer = sieve://127.0.0.1:4190; - //SOGoSMTPServer = 127.0.0.1; + //SOGoSMTPServer = "smtp://127.0.0.1"; //SOGoMailDomain = acme.com; //SOGoMailingMechanism = smtp; //SOGoForceExternalLoginWithEmail = NO; diff --git a/SoObjects/SOGo/SOGoDefaults.plist b/SoObjects/SOGo/SOGoDefaults.plist index 9eaa619c5..5d401e90c 100644 --- a/SoObjects/SOGo/SOGoDefaults.plist +++ b/SoObjects/SOGo/SOGoDefaults.plist @@ -29,7 +29,7 @@ SOGoLDAPContactInfoAttribute = "description"; SOGoMailingMechanism = "sendmail"; - SOGoSMTPServer = "localhost"; + SOGoSMTPServer = "smtp://localhost"; SOGoMailSpoolPath = "/var/spool/sogo"; SOGoWebAccessEnabled = YES; diff --git a/SoObjects/SOGo/SOGoDomainDefaults.m b/SoObjects/SOGo/SOGoDomainDefaults.m index 763eb6033..2e5c4149e 100644 --- a/SoObjects/SOGo/SOGoDomainDefaults.m +++ b/SoObjects/SOGo/SOGoDomainDefaults.m @@ -242,7 +242,15 @@ - (NSString *) smtpServer { - return [self stringForKey: @"SOGoSMTPServer"]; + NSString *server; + server = [self stringForKey: @"SOGoSMTPServer"]; + // backwards compatibility + if (![server hasPrefix: @"smtp://"] && + ![server hasPrefix: @"smtps://"]) + { + return [NSString stringWithFormat: @"smtp://%@", server]; + } + return server; } - (NSString *) smtpAuthenticationType diff --git a/SoObjects/SOGo/SOGoMailer.m b/SoObjects/SOGo/SOGoMailer.m index 03dcf6de9..eee02b544 100644 --- a/SoObjects/SOGo/SOGoMailer.m +++ b/SoObjects/SOGo/SOGoMailer.m @@ -25,6 +25,7 @@ #import #import +#import #import #import #import @@ -221,40 +222,27 @@ } - (NSException *) _smtpSendData: (NSData *) mailData - toRecipients: (NSArray *) recipients - sender: (NSString *) sender + toRecipients: (NSArray *) recipients + sender: (NSString *) sender withAuthenticator: (id ) authenticator inContext: (WOContext *) woContext { - NSString *currentTo, *host, *login, *password; - NGInternetSocketAddress *addr; + NSString *currentTo, *login, *password; NSMutableArray *toErrors; - NSEnumerator *addresses; + NSEnumerator *addresses; NGSmtpClient *client; NSException *result; - NSRange r; - unsigned int port; + NSURL * smtpUrl; - client = [NGSmtpClient smtpClient]; - host = smtpServer; result = nil; - port = 25; - // We check if there is a port specified in the smtpServer ivar value - r = [smtpServer rangeOfString: @":"]; - - if (r.length) - { - port = [[smtpServer substringFromIndex: r.location+1] intValue]; - host = [smtpServer substringToIndex: r.location]; - } + smtpUrl = [[[NSURL alloc] initWithString: smtpServer] autorelease]; - addr = [NGInternetSocketAddress addressWithPort: port - onHost: host]; + client = [NGSmtpClient clientWithURL: smtpUrl]; NS_DURING { - [client connectToAddress: addr]; + [client connect]; if ([authenticationType isEqualToString: @"plain"]) { /* XXX Allow static credentials by peeking at the classname */ @@ -302,7 +290,7 @@ @" (smtp) all recipients discarded"]; else if ([toErrors count] > 0) result = [NSException exceptionWithHTTPStatus: 500 - reason: [NSString stringWithFormat: + reason: [NSString stringWithFormat: @"cannot send message (smtp) - recipients discarded:\n%@", [toErrors componentsJoinedByString: @", "]]]; else @@ -318,7 +306,7 @@ } NS_HANDLER { - [self errorWithFormat: @"Could not connect to the SMTP server %@ on port %d", host, port]; + [self errorWithFormat: @"Could not connect to the SMTP server %@", smtpServer]; result = [NSException exceptionWithHTTPStatus: 500 reason: @"cannot send message:" @" (smtp) error when connecting"];