2007-08-09 22:54:52 +02:00
|
|
|
/* SOGoMailer.m - this file is part of SOGo
|
|
|
|
*
|
2015-01-23 21:32:22 +01:00
|
|
|
* Copyright (C) 2007-2015 Inverse inc.
|
2007-08-09 22:54:52 +02:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <NGMail/NGSendMail.h>
|
|
|
|
#import <NGMail/NGSmtpClient.h>
|
|
|
|
#import <NGMime/NGMimePartGenerator.h>
|
2009-12-26 17:01:08 +01:00
|
|
|
#import <NGStreams/NGInternetSocketAddress.h>
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
#import "NSString+Utilities.h"
|
2012-10-16 22:56:48 +02:00
|
|
|
#import "SOGoAuthenticator.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoDomainDefaults.h"
|
2013-08-27 19:02:06 +02:00
|
|
|
#import "SOGoStaticAuthenticator.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoSystemDefaults.h"
|
2012-10-16 22:56:48 +02:00
|
|
|
#import "SOGoUser.h"
|
2013-01-11 15:55:10 +01:00
|
|
|
#import "SOGoUserManager.h"
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoMailer.h"
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2017-03-08 14:52:40 +01:00
|
|
|
//
|
|
|
|
// Useful extension that comes from Pantomime which is also
|
|
|
|
// released under the LGPL. We should eventually merge
|
|
|
|
// this with the same category found in SOPE's NGSmtpClient.m
|
|
|
|
// or simply drop sope-mime in favor of Pantomime
|
|
|
|
//
|
|
|
|
@interface NSMutableData (DataCleanupExtension)
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (int) theIndex;
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString;
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
options: (unsigned int) theOptions
|
|
|
|
range: (NSRange) theRange;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableData (DataCleanupExtension)
|
|
|
|
|
|
|
|
- (unichar) characterAtIndex: (int) theIndex
|
|
|
|
{
|
|
|
|
const char *bytes;
|
|
|
|
int i, len;
|
|
|
|
|
|
|
|
len = [self length];
|
|
|
|
|
|
|
|
if (len == 0 || theIndex >= len)
|
|
|
|
{
|
|
|
|
[[NSException exceptionWithName: NSRangeException
|
|
|
|
reason: @"Index out of range."
|
|
|
|
userInfo: nil] raise];
|
|
|
|
|
|
|
|
return (unichar)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = [self bytes];
|
|
|
|
|
|
|
|
for (i = 0; i < theIndex; i++)
|
|
|
|
{
|
|
|
|
bytes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unichar)*bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
{
|
|
|
|
return [self rangeOfCString: theCString
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0,[self length])];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSRange) rangeOfCString: (const char *) theCString
|
|
|
|
options: (unsigned int) theOptions
|
|
|
|
range: (NSRange) theRange
|
|
|
|
{
|
|
|
|
const char *b, *bytes;
|
|
|
|
int i, len, slen;
|
|
|
|
|
|
|
|
if (!theCString)
|
|
|
|
{
|
|
|
|
return NSMakeRange(NSNotFound,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = [self bytes];
|
|
|
|
len = [self length];
|
|
|
|
slen = strlen(theCString);
|
|
|
|
|
|
|
|
b = bytes;
|
|
|
|
|
|
|
|
if (len > theRange.location + theRange.length)
|
|
|
|
{
|
|
|
|
len = theRange.location + theRange.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theOptions == NSCaseInsensitiveSearch)
|
|
|
|
{
|
|
|
|
i = theRange.location;
|
|
|
|
b += i;
|
|
|
|
|
|
|
|
for (; i <= len-slen; i++, b++)
|
|
|
|
{
|
|
|
|
if (!strncasecmp(theCString,b,slen))
|
|
|
|
{
|
|
|
|
return NSMakeRange(i,slen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = theRange.location;
|
|
|
|
b += i;
|
|
|
|
|
|
|
|
for (; i <= len-slen; i++, b++)
|
|
|
|
{
|
|
|
|
if (!memcmp(theCString,b,slen))
|
|
|
|
{
|
|
|
|
return NSMakeRange(i,slen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSMakeRange(NSNotFound,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-08-09 22:54:52 +02:00
|
|
|
@implementation SOGoMailer
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
+ (SOGoMailer *) mailerWithDomainDefaults: (SOGoDomainDefaults *) dd
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
return [[self alloc] initWithDomainDefaults: dd];
|
|
|
|
}
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (id) initWithDomainDefaults: (SOGoDomainDefaults *) dd
|
|
|
|
{
|
|
|
|
if ((self = [self init]))
|
|
|
|
{
|
|
|
|
ASSIGN (mailingMechanism, [dd mailingMechanism]);
|
|
|
|
ASSIGN (smtpServer, [dd smtpServer]);
|
2012-10-16 22:56:48 +02:00
|
|
|
ASSIGN (authenticationType,
|
|
|
|
[[dd smtpAuthenticationType] lowercaseString]);
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
2007-08-09 22:54:52 +02:00
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
return self;
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
mailingMechanism = nil;
|
|
|
|
smtpServer = nil;
|
2012-10-16 22:56:48 +02:00
|
|
|
authenticationType = nil;
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[mailingMechanism release];
|
|
|
|
[smtpServer release];
|
2012-10-16 22:56:48 +02:00
|
|
|
[authenticationType release];
|
2007-08-09 22:54:52 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _sendmailSendData: (NSData *) mailData
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NGSendMail *mailer;
|
|
|
|
|
|
|
|
mailer = [NGSendMail sharedSendMail];
|
|
|
|
if ([mailer isSendMailAvailable])
|
|
|
|
result = [mailer sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
|
|
|
sender: sender];
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" no sendmail binary!"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _sendMailData: (NSData *) mailData
|
|
|
|
withClient: (NGSmtpClient *) client
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
|
|
|
|
if ([client sendData: mailData])
|
|
|
|
result = nil;
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) failure when sending data"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) _smtpSendData: (NSData *) mailData
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
2012-10-16 22:56:48 +02:00
|
|
|
withAuthenticator: (id <SOGoAuthenticator>) authenticator
|
|
|
|
inContext: (WOContext *) woContext
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2012-10-16 22:56:48 +02:00
|
|
|
NSString *currentTo, *host, *login, *password;
|
2013-01-11 15:55:10 +01:00
|
|
|
NGInternetSocketAddress *addr;
|
2010-11-04 19:34:51 +01:00
|
|
|
NSMutableArray *toErrors;
|
2009-12-26 17:01:08 +01:00
|
|
|
NSEnumerator *addresses;
|
2007-08-09 22:54:52 +02:00
|
|
|
NGSmtpClient *client;
|
2013-01-11 15:55:10 +01:00
|
|
|
NSException *result;
|
2009-12-26 17:01:08 +01:00
|
|
|
NSRange r;
|
2010-11-04 19:34:51 +01:00
|
|
|
unsigned int port;
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
client = [NGSmtpClient smtpClient];
|
2009-12-26 17:01:08 +01:00
|
|
|
host = smtpServer;
|
2013-01-11 15:55:10 +01:00
|
|
|
result = nil;
|
2009-12-26 17:01:08 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = [NGInternetSocketAddress addressWithPort: port
|
|
|
|
onHost: host];
|
|
|
|
|
2010-11-10 21:12:54 +01:00
|
|
|
NS_DURING
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
2010-11-10 21:12:54 +01:00
|
|
|
[client connectToAddress: addr];
|
2012-10-16 22:56:48 +02:00
|
|
|
if ([authenticationType isEqualToString: @"plain"])
|
|
|
|
{
|
2013-08-27 19:02:06 +02:00
|
|
|
/* XXX Allow static credentials by peeking at the classname */
|
|
|
|
if ([authenticator isKindOfClass: [SOGoStaticAuthenticator class]])
|
|
|
|
login = [(SOGoStaticAuthenticator *)authenticator username];
|
|
|
|
else
|
|
|
|
login = [[SOGoUserManager sharedUserManager]
|
|
|
|
getExternalLoginForUID: [[authenticator userInContext: woContext] loginInDomain]
|
|
|
|
inDomain: [[authenticator userInContext: woContext] domain]];
|
2013-01-11 15:55:10 +01:00
|
|
|
|
2012-10-16 22:56:48 +02:00
|
|
|
password = [authenticator passwordInContext: woContext];
|
|
|
|
if ([login length] == 0
|
|
|
|
|| [login isEqualToString: @"anonymous"]
|
|
|
|
|| ![client plainAuthenticateUser: login
|
|
|
|
withPassword: password])
|
|
|
|
result = [NSException
|
|
|
|
exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) authentication failure"];
|
|
|
|
}
|
|
|
|
else if (authenticationType)
|
2011-04-05 17:10:51 +02:00
|
|
|
result = [NSException
|
|
|
|
exceptionWithHTTPStatus: 500
|
2012-10-16 22:56:48 +02:00
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" unsupported authentication method"];
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
if ([client mailFrom: sender])
|
|
|
|
{
|
|
|
|
toErrors = [NSMutableArray array];
|
|
|
|
addresses = [recipients objectEnumerator];
|
|
|
|
currentTo = [addresses nextObject];
|
|
|
|
while (currentTo)
|
|
|
|
{
|
|
|
|
if (![client recipientTo: [currentTo pureEMailAddress]])
|
|
|
|
{
|
|
|
|
[self logWithFormat: @"error with recipient '%@'", currentTo];
|
|
|
|
[toErrors addObject: [currentTo pureEMailAddress]];
|
|
|
|
}
|
|
|
|
currentTo = [addresses nextObject];
|
|
|
|
}
|
|
|
|
if ([toErrors count] == [recipients count])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) all recipients discarded"];
|
|
|
|
else if ([toErrors count] > 0)
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: [NSString stringWithFormat:
|
|
|
|
@"cannot send message (smtp) - recipients discarded:\n%@",
|
|
|
|
[toErrors componentsJoinedByString: @", "]]];
|
|
|
|
else
|
|
|
|
result = [self _sendMailData: mailData withClient: client];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException
|
|
|
|
exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: (smtp) originator not accepted"];
|
|
|
|
}
|
2007-08-09 22:54:52 +02:00
|
|
|
[client quit];
|
|
|
|
[client disconnect];
|
|
|
|
}
|
2010-11-10 21:12:54 +01:00
|
|
|
NS_HANDLER
|
|
|
|
{
|
2015-01-23 21:32:22 +01:00
|
|
|
[self errorWithFormat: @"Could not connect to the SMTP server %@ on port %d", host, port];
|
2010-11-10 21:12:54 +01:00
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message:"
|
|
|
|
@" (smtp) error when connecting"];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2007-08-09 22:54:52 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMailData: (NSData *) data
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
2012-10-16 22:56:48 +02:00
|
|
|
withAuthenticator: (id <SOGoAuthenticator>) authenticator
|
|
|
|
inContext: (WOContext *) woContext
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
|
|
|
|
if (![recipients count])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no recipients set"];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (![sender length])
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no sender set"];
|
|
|
|
else
|
|
|
|
{
|
2017-03-08 14:52:40 +01:00
|
|
|
NSMutableData *cleaned_message;
|
|
|
|
NSRange r1;
|
|
|
|
unsigned int limit;
|
|
|
|
|
|
|
|
//
|
|
|
|
// We now look for the Bcc: header. If it is present, we remove it.
|
|
|
|
// Some servers, like qmail, do not remove it automatically.
|
|
|
|
//
|
|
|
|
#warning FIXME - we should fix the case issue when we switch to Pantomime
|
|
|
|
cleaned_message = [NSMutableData dataWithData: data];
|
|
|
|
|
|
|
|
// We search only in the headers so we start at 0 until
|
|
|
|
// we find \r\n\r\n, which is the headers delimiter
|
|
|
|
r1 = [cleaned_message rangeOfCString: "\r\n\r\n"];
|
|
|
|
limit = r1.location-1;
|
|
|
|
|
|
|
|
// We check if the mail actually *starts* with the Bcc: header
|
|
|
|
r1 = [cleaned_message rangeOfCString: "Bcc: "
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0,5)];
|
|
|
|
|
|
|
|
// It does not, let's search in the entire headers
|
|
|
|
if (r1.location == NSNotFound)
|
2017-04-06 17:02:59 +02:00
|
|
|
{
|
|
|
|
r1 = [cleaned_message rangeOfCString: "\r\nBcc: "
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0,limit)];
|
2017-04-06 20:24:25 +02:00
|
|
|
if (r1.location != NSNotFound)
|
|
|
|
r1.location += 2;
|
2017-04-06 17:02:59 +02:00
|
|
|
}
|
2017-03-08 14:52:40 +01:00
|
|
|
|
|
|
|
if (r1.location != NSNotFound)
|
|
|
|
{
|
|
|
|
// We search for the first \r\n AFTER the Bcc: header and
|
|
|
|
// replace the whole thing with \r\n.
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = r1.location+7; i < limit; i++)
|
|
|
|
{
|
|
|
|
if ([cleaned_message characterAtIndex: i] == '\r' &&
|
|
|
|
(i+1 < limit && [cleaned_message characterAtIndex: i+1] == '\n') &&
|
|
|
|
(i+2 < limit && !isspace([cleaned_message characterAtIndex: i+2])))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
[cleaned_message replaceBytesInRange: NSMakeRange(r1.location, i-r1.location+2)
|
|
|
|
withBytes: NULL
|
|
|
|
length: 0];
|
|
|
|
}
|
|
|
|
|
2007-08-09 22:54:52 +02:00
|
|
|
if ([mailingMechanism isEqualToString: @"sendmail"])
|
2017-06-16 14:37:34 +02:00
|
|
|
result = [self _sendmailSendData: cleaned_message
|
2007-08-09 22:54:52 +02:00
|
|
|
toRecipients: recipients
|
|
|
|
sender: [sender pureEMailAddress]];
|
|
|
|
else
|
2017-06-16 14:37:34 +02:00
|
|
|
result = [self _smtpSendData: cleaned_message
|
2012-10-16 22:56:48 +02:00
|
|
|
toRecipients: recipients
|
|
|
|
sender: [sender pureEMailAddress]
|
|
|
|
withAuthenticator: authenticator
|
|
|
|
inContext: woContext];
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMimePart: (id <NGMimePart>) part
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
2012-10-16 22:56:48 +02:00
|
|
|
withAuthenticator: (id <SOGoAuthenticator>) authenticator
|
|
|
|
inContext: (WOContext *) woContext
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
|
|
|
NSData *mailData;
|
|
|
|
|
|
|
|
mailData = [[NGMimePartGenerator mimePartGenerator]
|
|
|
|
generateMimeFromPart: part];
|
|
|
|
|
|
|
|
return [self sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
2012-10-16 22:56:48 +02:00
|
|
|
sender: sender
|
|
|
|
withAuthenticator: authenticator
|
|
|
|
inContext: woContext];
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) sendMailAtPath: (NSString *) filename
|
|
|
|
toRecipients: (NSArray *) recipients
|
|
|
|
sender: (NSString *) sender
|
2012-10-16 22:56:48 +02:00
|
|
|
withAuthenticator: (id <SOGoAuthenticator>) authenticator
|
|
|
|
inContext: (WOContext *) woContext
|
2007-08-09 22:54:52 +02:00
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NSData *mailData;
|
|
|
|
|
|
|
|
mailData = [NSData dataWithContentsOfFile: filename];
|
|
|
|
if ([mailData length] > 0)
|
|
|
|
result = [self sendMailData: mailData
|
|
|
|
toRecipients: recipients
|
2012-10-16 22:56:48 +02:00
|
|
|
sender: sender
|
|
|
|
withAuthenticator: authenticator
|
|
|
|
inContext: woContext];
|
2007-08-09 22:54:52 +02:00
|
|
|
else
|
|
|
|
result = [NSException exceptionWithHTTPStatus: 500
|
|
|
|
reason: @"cannot send message: no data"
|
|
|
|
@" (missing or empty file?)"];
|
|
|
|
|
2012-10-16 22:56:48 +02:00
|
|
|
return result;
|
2007-08-09 22:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|