Skip to content

Commit

Permalink
Small code tidy-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenfra committed Aug 9, 2015
1 parent 04fcb2d commit 2edc9b2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
2 changes: 1 addition & 1 deletion PostgreSQL/Classes/PGServerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ - (void)runAction:(PGServerAction)action server:(PGServer *)server authorization

// Execute
@synchronized(server) {
[PGProcess runShellCommand:command authorization:authorization authStatus:&authStatus output:&output error:&error];
[PGProcess runShellCommand:command forRootUser:YES authorization:authorization authStatus:&authStatus output:&output error:&error];
}

result = output;
Expand Down
28 changes: 2 additions & 26 deletions PostgreSQL/Classes/Utils/PGLaunchd.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
#import "PGLaunchd.h"
#import <ServiceManagement/ServiceManagement.h>

#pragma mark - Interfaces

@interface PGLaunchd()
@end



#pragma mark - PGLaunchd

@implementation PGLaunchd
Expand Down Expand Up @@ -58,7 +51,7 @@ + (BOOL)startDaemonWithFile:(NSString *)file forRootUser:(BOOL)root authorizatio

NSString *command = [NSString stringWithFormat:@"launchctl load -F \"%@\"", file];
NSString *output = nil;
BOOL succeeded = [self runShellCommand:command forRootUser:root authorization:authorization authStatus:authStatus output:&output error:error];
BOOL succeeded = [PGProcess runShellCommand:command forRootUser:root authorization:authorization authStatus:authStatus output:&output error:error];
if (!succeeded) return NO;

return YES;
Expand All @@ -71,27 +64,10 @@ + (BOOL)stopDaemonWithName:(NSString *)name forRootUser:(BOOL)root authorization

NSString *command = [NSString stringWithFormat:@"launchctl remove \"%@\"", name];
NSString *output = nil;
BOOL succeeded = [self runShellCommand:command forRootUser:root authorization:authorization authStatus:authStatus output:&output error:error];
BOOL succeeded = [PGProcess runShellCommand:command forRootUser:root authorization:authorization authStatus:authStatus output:&output error:error];
if (!succeeded) return NO;

return YES;
}



#pragma mark Private

+ (BOOL)runShellCommand:(NSString *)command forRootUser:(BOOL)root authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus output:(NSString *__autoreleasing *)output error:(NSString *__autoreleasing *)error
{
if (root && !authorization) return NO;
if (!NonBlank(command)) return NO;

// Will run as root, but want user-specific launchd, so command must switch user
if (!root && authorization)
command = [NSString stringWithFormat:@"su \"%@\" -c '%@'", NSUserName(), command];

// Run
return [PGProcess runShellCommand:command authorization:authorization authStatus:authStatus output:output error:error];
}

@end
4 changes: 2 additions & 2 deletions PostgreSQL/Classes/Utils/PGProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
*
* @return YES if started without throwing an exception or authorization error
*/
+ (BOOL)startShellCommand:(NSString *)command authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus error:(NSString **)error;
+ (BOOL)startShellCommand:(NSString *)command forRootUser:(BOOL)root authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus error:(NSString **)error;
/**
* Runs command in shell with authorization (if not nil). Returns output.
*
* @return YES if ran without throwing an exception or authorization error
*/
+ (BOOL)runShellCommand:(NSString *)command authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus output:(NSString **)output error:(NSString **)error;
+ (BOOL)runShellCommand:(NSString *)command forRootUser:(BOOL)root authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus output:(NSString **)output error:(NSString **)error;

/**
* Starts executable without authorization. Does not wait for output.
Expand Down
39 changes: 23 additions & 16 deletions PostgreSQL/Classes/Utils/PGProcess.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ @implementation PGProcess
+ (NSString *)authorizingScript
{
static NSString *script;
static BOOL initialized;

// Find script in bundle
if (!initialized) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
script = [[NSBundle bundleForClass:[self class]] pathForResource:@"PGPrefsRunAsAdmin" ofType:@"scpt"];
if (!script) DLog(@"Cannot find resource PGPrefsRunAsAdmin.scpt!");
initialized = YES;
}
});

return script;
}
Expand All @@ -69,19 +66,16 @@ + (AuthorizationRights *)authorizationRights
static NSString *authorizingScript;
static AuthorizationItem authorizationItem;
static AuthorizationRights authorizationRights;
static BOOL initialized;

// Create rights
if (!initialized) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
authorizingScript = [self authorizingScript];
if (authorizingScript) {
AuthorizationItem item = {kAuthorizationRightExecute, authorizingScript.length, &authorizingScript, 0};
AuthorizationRights rights = {1, &authorizationItem};
authorizationItem = item;
authorizationRights = rights;
}
initialized = YES;
}
});

return authorizationRights.count == 0 ? nil : &authorizationRights;
}
Expand All @@ -102,15 +96,25 @@ + (NSString *)runShellCommand:(NSString *)command error:(NSString *__autoreleasi
}
+ (BOOL)runShellCommand:(NSString *)command output:(NSString *__autoreleasing *)output error:(NSString *__autoreleasing *)error
{
command = TrimToNil(command);
if (!command) return NO;

return [self runExecutable:@"/bin/bash" withArgs:@[@"-c", command] authorization:nil authStatus:nil output:output error:error];
}
+ (BOOL)startShellCommand:(NSString *)command authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus error:(NSString *__autoreleasing *)error
+ (BOOL)startShellCommand:(NSString *)command forRootUser:(BOOL)root authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus error:(NSString *__autoreleasing *)error
{
return [self runShellCommand:command authorization:authorization authStatus:authStatus output:nil error:error];
return [self runShellCommand:command forRootUser:root authorization:authorization authStatus:authStatus output:nil error:error];
}
+ (BOOL)runShellCommand:(NSString *)command authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus output:(NSString *__autoreleasing *)output error:(NSString *__autoreleasing *)error
+ (BOOL)runShellCommand:(NSString *)command forRootUser:(BOOL)root authorization:(AuthorizationRef)authorization authStatus:(OSStatus *)authStatus output:(NSString *__autoreleasing *)output error:(NSString *__autoreleasing *)error
{
if (!authorization) return [self runShellCommand:command output:output error:error];
command = TrimToNil(command);
if (!command) return NO;

// No authorization provided - run unauthorized or abort
if (!authorization) {
if (root) return NO;
else return [self runShellCommand:command output:output error:error];
}

// Program error - authorization script missing!
NSString *authorizingScript = [self authorizingScript];
Expand All @@ -119,6 +123,9 @@ + (BOOL)runShellCommand:(NSString *)command authorization:(AuthorizationRef)auth
return NO;
}

// Script always runs as root, so need to switch user
if (!root) command = [NSString stringWithFormat:@"su \"%@\" -c '%@'", NSUserName(), command];

// Execute
return [self runExecutable:@"/usr/bin/osascript" withArgs:@[authorizingScript, command] authorization:authorization authStatus:authStatus output:output error:error];
}
Expand Down

0 comments on commit 2edc9b2

Please sign in to comment.