diff --git a/SoObjects/SOGo/GNUmakefile b/SoObjects/SOGo/GNUmakefile index 43bce3e54..f9010db09 100644 --- a/SoObjects/SOGo/GNUmakefile +++ b/SoObjects/SOGo/GNUmakefile @@ -55,6 +55,7 @@ SOGo_HEADER_FILES = \ SOGoCASSession.h \ SOGoDAVAuthenticator.h \ SOGoProxyAuthenticator.h \ + SOGoStaticAuthenticator.h \ SOGoWebAuthenticator.h \ SOGoWebDAVAclManager.h \ SOGoWebDAVValue.h \ @@ -67,7 +68,9 @@ SOGo_HEADER_FILES = \ WORequest+SOGo.h \ WOResourceManager+SOGo.h \ WOResponse+SOGo.h \ - WOContext+SOGo.h + WOContext+SOGo.h \ + \ + SOGoCredentialsFile.h # daemon tool all:: @@ -124,6 +127,7 @@ SOGo_OBJC_FILES = \ SOGoCASSession.m \ SOGoDAVAuthenticator.m \ SOGoProxyAuthenticator.m \ + SOGoStaticAuthenticator.m \ SOGoWebAuthenticator.m \ SOGoWebDAVAclManager.m \ SOGoWebDAVValue.m \ @@ -136,7 +140,10 @@ SOGo_OBJC_FILES = \ WORequest+SOGo.m \ WOResourceManager+SOGo.m \ WOResponse+SOGo.m \ - WOContext+SOGo.m + WOContext+SOGo.m \ + \ + SOGoCredentialsFile.m + SOGo_RESOURCE_FILES = \ SOGoDefaults.plist \ diff --git a/SoObjects/SOGo/SOGoCredentialsFile.h b/SoObjects/SOGo/SOGoCredentialsFile.h new file mode 100644 index 000000000..b6946faba --- /dev/null +++ b/SoObjects/SOGo/SOGoCredentialsFile.h @@ -0,0 +1,43 @@ +/* SOGoCredentialsFile.h - this file is part of SOGo + * + * Copyright (C) 2013 Inverse inc. + * + * 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. + */ + +#ifndef SOGOCREDENTIALSFILE_H +#define SOGOCREDENTIALSFILE_H + +#import + +@interface SOGoCredentialsFile : NSObject +{ + NSString *_credentialsFile; + NSString *_username, *_password; +} + ++ (id) credentialsFromFile: (NSString *) file; + +- (id) initFromFile: (NSString *) file + withEncoding: (NSStringEncoding) enc; + +- (NSString *) username; +- (NSString *) password; +- (NSString *) credentialsFile; + +@end + +#endif /* SOGOCREDENTIALSFILE_H */ diff --git a/SoObjects/SOGo/SOGoCredentialsFile.m b/SoObjects/SOGo/SOGoCredentialsFile.m new file mode 100644 index 000000000..a13a7483e --- /dev/null +++ b/SoObjects/SOGo/SOGoCredentialsFile.m @@ -0,0 +1,113 @@ +/* SOGoCredentialsFile.m - this file is part of SOGo + * + * Copyright (C) 2013 Inverse inc. + * + * 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 +#import +#import + +#import "SOGoCredentialsFile.h" + +@implementation SOGoCredentialsFile + ++ (id) credentialsFromFile: (NSString *) file +{ + SOGoCredentialsFile *newCreds; + newCreds = [[self alloc] initFromFile: file + withEncoding: NSUTF8StringEncoding]; + [newCreds autorelease]; + return newCreds; +} + + +- (id) init +{ + if ((self = [super init])) + { + _username = nil; + _password = nil; + _credentialsFile = nil; + } + return self; +} + +- (void) dealloc +{ + [_username release]; + [_password release]; + [_credentialsFile release]; + [super dealloc]; +} + +- (id) initFromFile: (NSString *) file + withEncoding: (NSStringEncoding) enc +{ + id ret; + NSData *credentialsData; + NSRange r; + NSString *creds; + + ret = nil; + if (file) + { + if ((self = [self init])) + { + credentialsData = [NSData dataWithContentsOfFile: file]; + if (credentialsData == nil) + NSLog(@"Failed to load credentials file: %@", file); + else + { + creds = [[NSString alloc] initWithData: credentialsData + encoding: enc]; + [creds autorelease]; + creds = [creds stringByTrimmingCharactersInSet: + [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]]; + r = [creds rangeOfString: @":"]; + if (r.location == NSNotFound) + NSLog(@"Invalid credentials file content, missing ':' separator (%@)", file); + else + { + _username = [[creds substringToIndex: r.location] retain]; + _password = [[creds substringFromIndex: r.location+1] retain]; + _credentialsFile = [file retain]; + ret = self; + } + } + } + } + return ret; +} + + +- (NSString *) username +{ + return self->_username; +} + +- (NSString *) password +{ + return self->_password; +} + +- (NSString *) credentialsFile +{ + return self->_credentialsFile; +} + +@end diff --git a/SoObjects/SOGo/SOGoMailer.m b/SoObjects/SOGo/SOGoMailer.m index c19d303f9..bbf59540d 100644 --- a/SoObjects/SOGo/SOGoMailer.m +++ b/SoObjects/SOGo/SOGoMailer.m @@ -35,6 +35,7 @@ #import "NSString+Utilities.h" #import "SOGoAuthenticator.h" #import "SOGoDomainDefaults.h" +#import "SOGoStaticAuthenticator.h" #import "SOGoSystemDefaults.h" #import "SOGoUser.h" #import "SOGoUserManager.h" @@ -153,9 +154,13 @@ [client connectToAddress: addr]; if ([authenticationType isEqualToString: @"plain"]) { - login = [[SOGoUserManager sharedUserManager] - getExternalLoginForUID: [[authenticator userInContext: woContext] loginInDomain] - inDomain: [[authenticator userInContext: woContext] domain]]; + /* 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]]; password = [authenticator passwordInContext: woContext]; if ([login length] == 0 diff --git a/SoObjects/SOGo/SOGoStaticAuthenticator.h b/SoObjects/SOGo/SOGoStaticAuthenticator.h new file mode 100644 index 000000000..cb12c0276 --- /dev/null +++ b/SoObjects/SOGo/SOGoStaticAuthenticator.h @@ -0,0 +1,51 @@ +/* SOGoStaticAuthenticator.h - this file is part of SOGo + * + * Copyright (C) 2013 Inverse inc. + * + * 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. + */ + +#ifndef SOGOSTATICAUTHENTICATOR_H +#define SOGOSTATICAUTHENTICATOR_H + +#import + + +#import "SOGoAuthenticator.h" + +/* + SOGoStaticAuthenticator + +*/ + +@interface SOGoStaticAuthenticator : NSObject +{ + NSString *_username; + NSString *_password; +} + ++ (id) authenticatorWithUser: (NSString *) user + andPassword: (NSString *) password; + +- (id) initWithUser: (NSString *) user + andPassword: (NSString *) password; + +- (NSString *) username; + +@end + +#endif /* SOGOSTATICAUTHENTICATOR_H */ + diff --git a/SoObjects/SOGo/SOGoStaticAuthenticator.m b/SoObjects/SOGo/SOGoStaticAuthenticator.m new file mode 100644 index 000000000..af8daf0a2 --- /dev/null +++ b/SoObjects/SOGo/SOGoStaticAuthenticator.m @@ -0,0 +1,87 @@ +/* SOGoStaticAuthenticator.m - this file is part of SOGo + * + * Copyright (C) 2013 Inverse inc. + * + * 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 + +#import "SOGoStaticAuthenticator.h" + +@implementation SOGoStaticAuthenticator + ++ (id) authenticatorWithUser: (NSString *) user + andPassword: (NSString *) password +{ + SOGoStaticAuthenticator *newAuthenticator; + newAuthenticator = [[self alloc] initWithUser: user + andPassword: password]; + [newAuthenticator autorelease]; + return newAuthenticator; +} + +- (id) init +{ + if ((self = [super init])) + { + _username = nil; + _password = nil; + } + return self; +} + +- (void) dealloc +{ + [_username release]; + [_password release]; + [super dealloc]; +} + +- (id) initWithUser: (NSString *) user + andPassword: (NSString *) password; +{ + if ((self = [self init])) + { + _username = [user retain]; + _password = [password retain]; + } + return self; +} + +- (NSString *) passwordInContext: (WOContext *) context +{ + return _password; +} + +- (SOGoUser *) userInContext: (WOContext *) context +{ + return nil; +} + +- (NSString *) username +{ + return _username; +} + +- (NSString *) imapPasswordInContext: (WOContext *) context + forURL: (NSURL *) server + forceRenew: (BOOL) renew +{ + return _password; +} + +@end diff --git a/Tools/SOGoEAlarmsNotifier.h b/Tools/SOGoEAlarmsNotifier.h index 8cd964d9f..692b5260d 100644 --- a/Tools/SOGoEAlarmsNotifier.h +++ b/Tools/SOGoEAlarmsNotifier.h @@ -25,8 +25,11 @@ #import +#import + @interface SOGoEAlarmsNotifier : NSObject { + SOGoStaticAuthenticator *staticAuthenticator; } - (BOOL) run; diff --git a/Tools/SOGoEAlarmsNotifier.m b/Tools/SOGoEAlarmsNotifier.m index a8906c60a..81de6528d 100644 --- a/Tools/SOGoEAlarmsNotifier.m +++ b/Tools/SOGoEAlarmsNotifier.m @@ -1,6 +1,6 @@ /* SOGoEAlarmsNotifier.m - this file is part of SOGo * - * Copyright (C) 2011 Inverse inc. + * Copyright (C) 2011-2013 Inverse inc. * * Author: Wolfgang Sourdeau * @@ -27,6 +27,7 @@ #import #import #import +#import #import #import @@ -38,12 +39,14 @@ #import #import +#import "SOGo/SOGoCredentialsFile.h" #import #import #import -#import #import #import +#import +#import #import #import #import @@ -52,6 +55,21 @@ @implementation SOGoEAlarmsNotifier +- (id) init +{ + if ((self = [super init])) + { + staticAuthenticator = nil; + } + return self; +} + +- (void) dealloc +{ + [staticAuthenticator release]; + [super dealloc]; +} + - (NSString *) _messageID { static int pid = 0; @@ -124,11 +142,10 @@ [message setBody: content]; to = [attendee rfc822Email]; - /* TODO: SMTP authentication for services */ [mailer sendMimePart: message toRecipients: [NSArray arrayWithObject: to] sender: from - withAuthenticator: nil inContext: nil]; + withAuthenticator: staticAuthenticator inContext: nil]; } - (void) _processAlarm: (iCalAlarm *) alarm @@ -160,14 +177,45 @@ withMailer: mailer]; } +- (void) usage +{ + fprintf (stderr, "sogo-ealarms-notify [-p credentialFile] [-h]\n\n" + " -p credentialFile Specify the file containing credentials to use for SMTP AUTH\n" + " The file should contain a single line:\n" + " username:password\n" + " -h This message\n" + "\n" + "This program should be configured to run every minute from a crontab.\n"); +} + - (BOOL) run { SOGoEMailAlarmsManager *eaMgr; + SOGoCredentialsFile *cf; NSCalendarDate *startDate, *toDate; NSArray *alarms; NSMutableArray *owners; + NSString *credsFilename; int count, max; + if ([[NSUserDefaults standardUserDefaults] stringForKey: @"h"]) + { + [self usage]; + return YES; + } + + credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFilename) + { + cf = [SOGoCredentialsFile credentialsFromFile: credsFilename]; + if (!cf) + return NO; + staticAuthenticator = + [SOGoStaticAuthenticator authenticatorWithUser: [cf username] + andPassword: [cf password]]; + [staticAuthenticator retain]; + } + [[SOGoProductLoader productLoader] loadProducts: [NSArray arrayWithObject: @"Appointments.SOGo"]]; diff --git a/Tools/SOGoToolExpireAutoReply.m b/Tools/SOGoToolExpireAutoReply.m index 5e65e6a82..4cab4f7c9 100644 --- a/Tools/SOGoToolExpireAutoReply.m +++ b/Tools/SOGoToolExpireAutoReply.m @@ -1,6 +1,6 @@ /* SOGoToolUserPreferences.m - this file is part of SOGo * - * Copyright (C) 2011 Inverse inc. + * Copyright (C) 2011-2013 Inverse inc. * * Author: Francis Lachapelle * @@ -37,10 +37,11 @@ #import #import -#import -#import -#import +#import "SOGo/SOGoCredentialsFile.h" #import +#import +#import +#import #import "SOGoTool.h" @@ -180,9 +181,9 @@ - (BOOL) run { - NSData *credsData; NSRange r; - NSString *creds, *credsFile, *authname, *authpwd; + NSString *creds, *credsFilename, *authname, *authpwd; + SOGoCredentialsFile *cf; BOOL rc; int max; @@ -192,41 +193,34 @@ authpwd = nil; rc = NO; - credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; - if (credsFile) + credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFilename) { - credsData = [NSData dataWithContentsOfFile: credsFile]; - if (credsData == nil) - { - NSLog(@"Error reading credential file '%@'", credsFile); - return NO; - } - creds = [[NSString alloc] initWithData: credsData - encoding: NSUTF8StringEncoding]; - [creds autorelease]; - creds = [creds stringByTrimmingCharactersInSet: - [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]]; + cf = [SOGoCredentialsFile credentialsFromFile: credsFilename]; + authname = [cf username]; + authpwd = [cf password]; } + /* DEPRECATED: this is only kept around to avoid breaking existing setups */ if (max > 0) { /* assume we got the creds directly on the cli */ creds = [sanitizedArguments objectAtIndex: 0]; + if (creds) + { + r = [creds rangeOfString: @":"]; + if (r.location == NSNotFound) + { + NSLog(@"Invalid credential string format (user:pass)"); + } + else + { + authname = [creds substringToIndex: r.location]; + authpwd = [creds substringFromIndex: r.location+1]; + } + } } - if (creds) - { - r = [creds rangeOfString: @":"]; - if (r.location == NSNotFound) - { - NSLog(@"Invalid credential string format (user:pass)"); - } - else - { - authname = [creds substringToIndex: r.location]; - authpwd = [creds substringFromIndex: r.location+1]; - } - } if (authname && authpwd) { diff --git a/Tools/SOGoToolUserPreferences.m b/Tools/SOGoToolUserPreferences.m index 39118e777..3d1f982a2 100644 --- a/Tools/SOGoToolUserPreferences.m +++ b/Tools/SOGoToolUserPreferences.m @@ -1,6 +1,6 @@ /* SOGoToolUserPreferences.m - this file is part of SOGo * - * Copyright (C) 2011 Inverse inc. + * Copyright (C) 2011-2013 Inverse inc. * * Author: Ludovic Marcotte * @@ -28,6 +28,7 @@ #import #import +#import "SOGo/SOGoCredentialsFile.h" #import #import #import @@ -109,34 +110,17 @@ typedef enum [theKey caseInsensitiveCompare: @"Vacation"] == NSOrderedSame) { /* credentials file handling */ - NSData *credsData; - NSRange r; - NSString *credsFile, *creds, *authname, *authpwd; - authname = nil; - authpwd = nil; + NSString *credsFilename, *authname, *authpwd; + SOGoCredentialsFile *cf; - - credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; - if (credsFile) + credsFilename = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"]; + if (credsFilename) { - /* TODO: add back support for user:pwd here? */ - credsData = [NSData dataWithContentsOfFile: credsFile]; - if (credsData == nil) - { - NSLog(@"Error reading credential file '%@'", credsFile); - return NO; - } - - creds = [[NSString alloc] initWithData: credsData - encoding: NSUTF8StringEncoding]; - [creds autorelease]; - creds = [creds stringByTrimmingCharactersInSet: - [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]]; - - r = [creds rangeOfString: @":"]; - authname = [creds substringToIndex: r.location]; - authpwd = [creds substringFromIndex: r.location+1]; + cf = [SOGoCredentialsFile credentialsFromFile: credsFilename]; + authname = [cf username]; + authpwd = [cf password]; } + if (authname == nil || authpwd == nil) { NSLog(@"To update Sieve scripts, you must provide the \"-p credentialFile\" parameter");