Merge pull request #7 from inverse-inc/fix/1955_sogo-tool-passwd-handling
Fix/1955 sogo tool passwd handlingpull/8/head
commit
8ce4b89656
|
@ -31,6 +31,7 @@
|
|||
{
|
||||
BOOL verbose;
|
||||
NSArray *arguments;
|
||||
NSArray *sanitizedArguments; /* arguments w/o args from NSArgumentDomain */
|
||||
}
|
||||
|
||||
+ (NSString *) command;
|
||||
|
@ -40,6 +41,7 @@
|
|||
verbose: (BOOL) isVerbose;
|
||||
|
||||
- (void) setArguments: (NSArray *) newArguments;
|
||||
- (void) setSanitizedArguments: (NSArray *) newArguments;
|
||||
- (void) setVerbose: (BOOL) newVerbose;
|
||||
- (BOOL) run;
|
||||
|
||||
|
|
|
@ -21,7 +21,10 @@
|
|||
*/
|
||||
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSCharacterSet.h>
|
||||
#import <Foundation/NSDictionary.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSUserDefaults.h>
|
||||
|
||||
#import "SOGoTool.h"
|
||||
|
||||
|
@ -49,6 +52,7 @@
|
|||
[instance autorelease];
|
||||
|
||||
[instance setArguments: toolArguments];
|
||||
[instance setSanitizedArguments: toolArguments];
|
||||
[instance setVerbose: isVerbose];
|
||||
|
||||
return [instance run];
|
||||
|
@ -59,6 +63,7 @@
|
|||
if ((self = [super init]))
|
||||
{
|
||||
arguments = nil;
|
||||
sanitizedArguments = nil;
|
||||
verbose = NO;
|
||||
}
|
||||
|
||||
|
@ -70,6 +75,51 @@
|
|||
ASSIGN (arguments, newArguments);
|
||||
}
|
||||
|
||||
- (void) setSanitizedArguments: (NSArray *) newArguments
|
||||
{
|
||||
NSString *argsString = [newArguments componentsJoinedByString:@" "];
|
||||
NSDictionary *cliArguments;
|
||||
NSArray *keys;
|
||||
|
||||
int i;
|
||||
|
||||
argsString = [newArguments componentsJoinedByString:@" "];
|
||||
|
||||
/* Remove NSArgumentDomain -key value from the arguments */
|
||||
cliArguments = [[NSUserDefaults standardUserDefaults]
|
||||
volatileDomainForName:NSArgumentDomain];
|
||||
keys = [cliArguments allKeys];
|
||||
for (i=0; i < [keys count]; i++)
|
||||
{
|
||||
NSString *k = [keys objectAtIndex: i];
|
||||
NSString *v = [cliArguments objectForKey:k];
|
||||
NSString *argPair = [NSString stringWithFormat:@"-%@ %@", k, v];
|
||||
argsString = [argsString stringByReplacingOccurrencesOfString: argPair
|
||||
withString: @""];
|
||||
}
|
||||
if ([argsString length])
|
||||
{
|
||||
/* dance to compact whitespace */
|
||||
NSArray *wordsWP = [argsString componentsSeparatedByCharactersInSet:
|
||||
[NSCharacterSet whitespaceCharacterSet]];
|
||||
NSMutableArray *words = [NSMutableArray array];
|
||||
for (NSString *word in wordsWP)
|
||||
{
|
||||
if([word length] > 1)
|
||||
{
|
||||
[words addObject:word];
|
||||
}
|
||||
}
|
||||
argsString = [words componentsJoinedByString:@" "];
|
||||
ASSIGN (sanitizedArguments, [argsString componentsSeparatedByString:@" "]);
|
||||
}
|
||||
else
|
||||
{
|
||||
DESTROY(sanitizedArguments);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (void) setVerbose: (BOOL) newVerbose
|
||||
{
|
||||
verbose = newVerbose;
|
||||
|
@ -78,6 +128,7 @@
|
|||
- (void) dealloc
|
||||
{
|
||||
[arguments release];
|
||||
[sanitizedArguments release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
|
|
|
@ -22,9 +22,11 @@
|
|||
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSCalendarDate.h>
|
||||
#import <Foundation/NSCharacterSet.h>
|
||||
#import <Foundation/NSData.h>
|
||||
#import <Foundation/NSDictionary.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSUserDefaults.h>
|
||||
#import <Foundation/NSValue.h>
|
||||
|
||||
#import <GDLAccess/EOAdaptorChannel.h>
|
||||
|
@ -63,9 +65,11 @@
|
|||
|
||||
- (void) usage
|
||||
{
|
||||
fprintf (stderr, "expire-autoreply authname:authpassword\n\n"
|
||||
" authname administrator username of the Sieve server\n"
|
||||
" authpassword administrator password of the Sieve server\n\n"
|
||||
fprintf (stderr, "expire-autoreply -p credentialFile\n\n"
|
||||
" -p credentialFile Specify the file containing the sieve admin credentials\n"
|
||||
" The file should contain a single line:\n"
|
||||
" username:password\n"
|
||||
"\n"
|
||||
"The expire-autoreply action should be configured as a daily cronjob.\n");
|
||||
}
|
||||
|
||||
|
@ -123,12 +127,20 @@
|
|||
now = [[NSCalendarDate calendarDate] timeIntervalSince1970];
|
||||
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
||||
profileURL = [sd profileURL];
|
||||
if (profileURL)
|
||||
if (!profileURL)
|
||||
{
|
||||
NSLog(@"Couldn't obtain the profileURL. (Hint: SOGoProfileURL)");
|
||||
}
|
||||
else
|
||||
{
|
||||
tableURL = [[NSURL alloc] initWithString: profileURL];
|
||||
cm = [GCSChannelManager defaultChannelManager];
|
||||
channel = [cm acquireOpenChannelForURL: tableURL];
|
||||
if (channel)
|
||||
if (!channel)
|
||||
{
|
||||
NSLog(@"Couldn't acquire channel for profileURL");
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = [NSString stringWithFormat: @"SELECT c_uid, c_defaults FROM %@",
|
||||
[tableURL gcsTableName]];
|
||||
|
@ -166,29 +178,59 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
- (BOOL) run
|
||||
{
|
||||
NSError *err;
|
||||
NSRange r;
|
||||
NSString *creds, *authname, *authpwd;
|
||||
NSString *creds, *credsFile, *authname, *authpwd;
|
||||
BOOL rc;
|
||||
int max;
|
||||
|
||||
max = [arguments count];
|
||||
max = [sanitizedArguments count];
|
||||
creds = nil;
|
||||
authname = nil;
|
||||
authpwd = nil;
|
||||
rc = NO;
|
||||
|
||||
credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"];
|
||||
if (credsFile)
|
||||
{
|
||||
creds = [NSString stringWithContentsOfFile: credsFile
|
||||
encoding: NSUTF8StringEncoding
|
||||
error: &err];
|
||||
if (!creds)
|
||||
{
|
||||
NSLog(@"Error reading credential file '%@': %@", credsFile, err);
|
||||
}
|
||||
creds = [creds stringByTrimmingCharactersInSet:
|
||||
[NSCharacterSet newlineCharacterSet]];
|
||||
}
|
||||
|
||||
if (max > 0)
|
||||
{
|
||||
creds = [arguments objectAtIndex: 0];
|
||||
/* assume we got the creds directly on the cli */
|
||||
creds = [sanitizedArguments objectAtIndex: 0];
|
||||
}
|
||||
|
||||
if (creds)
|
||||
{
|
||||
r = [creds rangeOfString: @":"];
|
||||
if (r.location != NSNotFound)
|
||||
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)
|
||||
{
|
||||
[self expireAutoReplyWithUsername: authname andPassword: authpwd];
|
||||
rc = YES;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rc)
|
||||
[self usage];
|
||||
|
|
|
@ -21,9 +21,11 @@
|
|||
*/
|
||||
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSCharacterSet.h>
|
||||
#import <Foundation/NSData.h>
|
||||
#import <Foundation/NSDictionary.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSUserDefaults.h>
|
||||
|
||||
#import <SOGo/NSString+Utilities.h>
|
||||
#import <SOGo/SOGoUser.h>
|
||||
|
@ -62,9 +64,12 @@ typedef enum
|
|||
|
||||
- (void) usage
|
||||
{
|
||||
fprintf (stderr, "user-preferences get|set|unset defaults|settings user [authname:authpassword] key [value|-f filename]\n\n"
|
||||
fprintf (stderr, "user-preferences get|set|unset defaults|settings user [-p credentialFile] key [value|-f filename]\n\n"
|
||||
" user the user of whom to set the defaults/settings key/value\n"
|
||||
" value the JSON-formatted value of the key\n\n"
|
||||
" -p credentialFile Specify the file containing the sieve admin credentials\n"
|
||||
" The file should contain a single line:\n"
|
||||
" username:password\n"
|
||||
" Examples:\n"
|
||||
" sogo-tool user-preferences get defaults janedoe SOGoLanguage\n"
|
||||
" sogo-tool user-preferences unset settings janedoe Mail\n"
|
||||
|
@ -83,7 +88,8 @@ typedef enum
|
|||
else if ([theString caseInsensitiveCompare: @"set"] == NSOrderedSame)
|
||||
return UserPreferencesSet;
|
||||
else if ([theString caseInsensitiveCompare: @"unset"] == NSOrderedSame)
|
||||
return UserPreferencesUnset; }
|
||||
return UserPreferencesUnset;
|
||||
}
|
||||
|
||||
return UserPreferencesUnknown;
|
||||
}
|
||||
|
@ -97,22 +103,44 @@ typedef enum
|
|||
- (BOOL) _updateSieveScripsForkey: (NSString *) theKey
|
||||
manager: (SOGoSieveManager *) theManager
|
||||
login: (NSString *) theLogin
|
||||
authname: (NSString *) theAuthName
|
||||
password: (NSString *) thePassword
|
||||
{
|
||||
if ([theKey caseInsensitiveCompare: @"Forward"] == NSOrderedSame ||
|
||||
[theKey caseInsensitiveCompare: @"SOGoSieveFilters"] == NSOrderedSame ||
|
||||
[theKey caseInsensitiveCompare: @"Vacation"] == NSOrderedSame)
|
||||
{
|
||||
if ([theAuthName length] == 0 || [thePassword length] == 0)
|
||||
/* credentials file handling */
|
||||
NSRange r;
|
||||
NSString *credsFile, *creds, *authname, *authpwd;
|
||||
authname = nil;
|
||||
authpwd = nil;
|
||||
|
||||
credsFile = [[NSUserDefaults standardUserDefaults] stringForKey: @"p"];
|
||||
if (credsFile)
|
||||
{
|
||||
NSLog(@"To update Sieve scripts, you must provide the \"authname:password\" parameter");
|
||||
/* TODO: add back support for user:pwd here? */
|
||||
creds = [NSString stringWithContentsOfFile: credsFile
|
||||
encoding: NSUTF8StringEncoding
|
||||
error: NULL];
|
||||
if (creds == nil)
|
||||
{
|
||||
NSLog(@"Error reading credential file '%@'", credsFile);
|
||||
return NO;
|
||||
}
|
||||
creds = [creds stringByTrimmingCharactersInSet:
|
||||
[NSCharacterSet newlineCharacterSet]];
|
||||
r = [creds rangeOfString: @":"];
|
||||
authname = [creds substringToIndex: r.location];
|
||||
authpwd = [creds substringFromIndex: r.location+1];
|
||||
}
|
||||
if (authname == nil || authpwd == nil)
|
||||
{
|
||||
NSLog(@"To update Sieve scripts, you must provide the \"-p credentialFile\" parameter");
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [theManager updateFiltersForLogin: theLogin
|
||||
authname: theAuthName
|
||||
password: thePassword
|
||||
authname: authname
|
||||
password: authpwd
|
||||
account: nil];
|
||||
}
|
||||
|
||||
|
@ -123,15 +151,15 @@ typedef enum
|
|||
|
||||
- (BOOL) run
|
||||
{
|
||||
NSString *userId, *type, *key;
|
||||
NSString *userId, *type, *key, *value;
|
||||
NSString *jsonValueFile;
|
||||
SOGoUserPreferencesCommand cmd;
|
||||
id o;
|
||||
|
||||
NSRange r;
|
||||
BOOL rc;
|
||||
int max;
|
||||
|
||||
max = [arguments count];
|
||||
max = [sanitizedArguments count];
|
||||
rc = NO;
|
||||
|
||||
if (max > 3)
|
||||
|
@ -140,13 +168,11 @@ typedef enum
|
|||
SOGoSieveManager *manager;
|
||||
SOGoUser *user;
|
||||
|
||||
cmd = [self _cmdFromString: [arguments objectAtIndex: 0]];
|
||||
cmd = [self _cmdFromString: [sanitizedArguments objectAtIndex: 0]];
|
||||
|
||||
if (cmd != UserPreferencesUnknown)
|
||||
{
|
||||
type = [arguments objectAtIndex: 1];
|
||||
userId = [arguments objectAtIndex: 2];
|
||||
key = [arguments objectAtIndex: 3];
|
||||
type = [sanitizedArguments objectAtIndex: 1];
|
||||
userId = [sanitizedArguments objectAtIndex: 2];
|
||||
key = [sanitizedArguments objectAtIndex: 3];
|
||||
|
||||
user = [SOGoUser userWithLogin: userId];
|
||||
manager = [SOGoSieveManager sieveManagerForUser: user];
|
||||
|
@ -156,74 +182,55 @@ typedef enum
|
|||
else
|
||||
source = [user userSettings];
|
||||
|
||||
if (cmd == UserPreferencesGet)
|
||||
switch (cmd)
|
||||
{
|
||||
case UserPreferencesGet:
|
||||
o = [source objectForKey: key];
|
||||
|
||||
if (o)
|
||||
{
|
||||
printf("%s: %s\n", [key UTF8String], [[o jsonRepresentation] UTF8String]);
|
||||
else
|
||||
NSLog(@"Value for key \"%@\" not found in %@", key, type);
|
||||
|
||||
rc = YES;
|
||||
}
|
||||
else
|
||||
{
|
||||
NSString *authname, *authpwd, *value;
|
||||
NSData *data;
|
||||
int i;
|
||||
|
||||
authname = @"";
|
||||
authpwd = @"";
|
||||
value = @"";
|
||||
NSLog(@"Value for key \"%@\" not found in %@", key, type);
|
||||
return rc;
|
||||
}
|
||||
break;
|
||||
|
||||
case UserPreferencesSet:
|
||||
if (max > 4)
|
||||
{
|
||||
r = [[arguments objectAtIndex: 3] rangeOfString: @":"];
|
||||
if (r.location == NSNotFound)
|
||||
{
|
||||
i = 3;
|
||||
/* value specified on command line */
|
||||
value = [sanitizedArguments objectAtIndex: 4];
|
||||
}
|
||||
else
|
||||
{
|
||||
authname = [[arguments objectAtIndex: 3] substringToIndex: r.location];
|
||||
authpwd = [[arguments objectAtIndex: 3] substringFromIndex: r.location+1];
|
||||
i = 4;
|
||||
/* value is to be found in file specified with -f filename */
|
||||
jsonValueFile = [[NSUserDefaults standardUserDefaults]
|
||||
stringForKey: @"f"];
|
||||
|
||||
if (jsonValueFile == nil)
|
||||
{
|
||||
NSLog(@"No value specified, aborting");
|
||||
[self usage];
|
||||
return rc;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
key = [arguments objectAtIndex: i++];
|
||||
|
||||
if (max > i)
|
||||
NSData *data = [NSData dataWithContentsOfFile: jsonValueFile];
|
||||
if (data == nil)
|
||||
{
|
||||
value = [arguments objectAtIndex: i++];
|
||||
if ([value caseInsensitiveCompare: @"-f"] == NSOrderedSame)
|
||||
{
|
||||
if (max > i)
|
||||
{
|
||||
data = [NSData dataWithContentsOfFile: [arguments objectAtIndex: i]];
|
||||
NSLog(@"Error reading file '%@'", jsonValueFile);
|
||||
[self usage];
|
||||
return rc;
|
||||
}
|
||||
value = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
|
||||
[value autorelease];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cmd == UserPreferencesUnset)
|
||||
{
|
||||
key = [arguments objectAtIndex: 3];
|
||||
}
|
||||
else
|
||||
{
|
||||
key = [arguments objectAtIndex: 3];
|
||||
value = [arguments objectAtIndex: 4];
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd == UserPreferencesUnset)
|
||||
[source removeObjectForKey: key];
|
||||
else
|
||||
{
|
||||
o = [value objectFromJSONString];
|
||||
|
||||
//
|
||||
|
@ -251,16 +258,35 @@ typedef enum
|
|||
[source setObject: o forKey: key];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"Invalid JSON input - no changes performed in the database. The supplied value was: %@", value);
|
||||
[self usage];
|
||||
return rc;
|
||||
}
|
||||
|
||||
[source synchronize];
|
||||
rc = [self _updateSieveScripsForkey: key
|
||||
manager: manager
|
||||
login: userId
|
||||
authname: authname
|
||||
password: authpwd];
|
||||
}
|
||||
login: userId];
|
||||
if (rc)
|
||||
[source synchronize];
|
||||
else
|
||||
NSLog(@"Error updating sieve script, not updating database");
|
||||
|
||||
break;
|
||||
|
||||
case UserPreferencesUnset:
|
||||
[source removeObjectForKey: key];
|
||||
rc = [self _updateSieveScripsForkey: key
|
||||
manager: manager
|
||||
login: userId];
|
||||
if (rc)
|
||||
[source synchronize];
|
||||
else
|
||||
NSLog(@"Error updating sieve script, not updating database");
|
||||
|
||||
break;
|
||||
case UserPreferencesUnknown:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue