expire-autoreply: read sieve creds from a file
to specify the credential file, use: -p filename The format of the file is: username:password Fixes: 1955 Also, add logging for some error conditions The program still supports user:password on the cli since this is run by cron and existing setups would breakpull/7/head
parent
6f8d8394df
commit
a75c797553
|
@ -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,10 +65,12 @@
|
|||
|
||||
- (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"
|
||||
"The expire-autoreply action should be configured as a daily cronjob.\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");
|
||||
}
|
||||
|
||||
- (BOOL) removeAutoReplyForLogin: (NSString *) theLogin
|
||||
|
@ -123,12 +127,20 @@
|
|||
now = [[NSCalendarDate calendarDate] timeIntervalSince1970];
|
||||
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
||||
profileURL = [sd profileURL];
|
||||
if (profileURL)
|
||||
if (!profileURL)
|
||||
{
|
||||
NSLog(@"No profileURL");
|
||||
}
|
||||
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,30 +178,60 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
- (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 == nil)
|
||||
{
|
||||
NSLog(@"Error reading credential file '%@': %@", credsFile, err);
|
||||
}
|
||||
creds = [creds stringByTrimmingCharactersInSet:
|
||||
[NSCharacterSet newlineCharacterSet]];
|
||||
}
|
||||
|
||||
if (max > 0)
|
||||
{
|
||||
creds = [arguments objectAtIndex: 0];
|
||||
r = [creds rangeOfString: @":"];
|
||||
if (r.location != NSNotFound)
|
||||
{
|
||||
authname = [creds substringToIndex: r.location];
|
||||
authpwd = [creds substringFromIndex: r.location+1];
|
||||
[self expireAutoReplyWithUsername: authname andPassword: authpwd];
|
||||
rc = YES;
|
||||
}
|
||||
/* assume we got the creds directly on the cli */
|
||||
creds = [sanitizedArguments objectAtIndex: 0];
|
||||
}
|
||||
|
||||
if (creds != nil)
|
||||
{
|
||||
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 != nil && authpwd != nil)
|
||||
{
|
||||
[self expireAutoReplyWithUsername: authname andPassword: authpwd];
|
||||
rc = YES;
|
||||
}
|
||||
|
||||
if (!rc)
|
||||
[self usage];
|
||||
|
||||
|
|
Loading…
Reference in New Issue