santactl: Simplify protocol and daemonConn handling

This commit is contained in:
Russell Hancox
2015-01-20 18:58:40 -05:00
parent 407466cd5f
commit ccd871cfdd
8 changed files with 38 additions and 30 deletions

View File

@@ -31,15 +31,6 @@
/// A longer description of the command when the user runs <tt>santactl help x</tt>
+ (NSString *)longHelpText;
@optional
/// Either of the following two methods needs to be implemented
/// Called when the user is running the command
/// @param arguments an array of arguments passed in
/// @note This method (or one of the methods it calls) is responsible for calling exit().
+ (void)runWithArguments:(NSArray *)arguments;
/**
* Called when the user is running the command
* @param arguments an array of arguments passed in
@@ -74,12 +65,12 @@
/// Returns YES if @c commandName exists.
+ (BOOL)hasCommandWithName:(NSString *)commandName;
+ (int)runCommandWithName:(NSString *)commandName arguments:(NSArray *)arguments;
/**
* Runs the given command with the given arguments.
* @param commandName the name of a previously-registered command
* @param arguments an array of arguments to pass to the command
*/
+ (void)runCommandWithName:(NSString *)commandName arguments:(NSArray *)arguments;
@end

View File

@@ -90,26 +90,23 @@ static NSMutableDictionary *registeredCommands;
return ([registeredCommands objectForKey:commandName] != nil);
}
+ (int)runCommandWithName:(NSString *)commandName arguments:(NSArray *)arguments {
+ (void)runCommandWithName:(NSString *)commandName arguments:(NSArray *)arguments {
Class<SNTCommand> command = registeredCommands[commandName];
if (command) {
if ([command requiresRoot] && getuid() != 0) {
printf("The command '%s' requires root privileges.\n", [commandName UTF8String]);
return 2;
}
if ([(id)command respondsToSelector:@selector(runWithArguments:daemonConnection:)]) {
[command runWithArguments:arguments daemonConnection:[self connectToDaemon]];
} else if ([(id)command respondsToSelector:@selector(runWithArguments:)]) {
[command runWithArguments:arguments];
} else {
printf("The command '%s' has not been implemented correctly.\n", [commandName UTF8String]);
}
// The command is responsible for quitting.
[[NSRunLoop mainRunLoop] run];
if ([command requiresRoot] && getuid() != 0) {
printf("The command '%s' requires root privileges.\n", [commandName UTF8String]);
exit(2);
}
return 128;
SNTXPCConnection *daemonConn;
if ([command requiresDaemonConn]) {
daemonConn = [self connectToDaemon];
}
[command runWithArguments:arguments daemonConnection:daemonConn];
// The command is responsible for quitting.
[[NSRunLoop mainRunLoop] run];
}
@end

View File

@@ -31,6 +31,10 @@ REGISTER_COMMAND_NAME(@"binaryinfo");
return NO;
}
+ (BOOL)requiresDaemonConn {
return NO;
}
+ (NSString *)shortHelpText {
return @"Prints information about the given binary.";
}
@@ -40,7 +44,7 @@ REGISTER_COMMAND_NAME(@"binaryinfo");
@"This includes SHA-1, code signing information and the type of binary");
}
+ (void)runWithArguments:(NSArray *)arguments {
+ (void)runWithArguments:(NSArray *)arguments daemonConnection:(SNTXPCConnection *)daemonConn {
NSString *filePath = [arguments firstObject];
if (!filePath) {

View File

@@ -29,6 +29,10 @@ REGISTER_COMMAND_NAME(@"flushcache");
return YES;
}
+ (BOOL)requiresDaemonConn {
return YES;
}
+ (NSString *)shortHelpText {
return @"Flush the kernel cache";
}

View File

@@ -75,6 +75,6 @@ int main(int argc, const char *argv[]) {
return 128;
}
return [SNTCommandController runCommandWithName:commandName arguments:arguments];
[SNTCommandController runCommandWithName:commandName arguments:arguments];
}
}

View File

@@ -36,7 +36,11 @@
REGISTER_COMMAND_NAME(@"rule");
+ (BOOL)requiresRoot {
return TRUE;
return YES;
}
+ (BOOL)requiresDaemonConn {
return YES;
}
+ (NSString *)shortHelpText {

View File

@@ -33,6 +33,10 @@ REGISTER_COMMAND_NAME(@"status");
return NO;
}
+ (BOOL)requiresDaemonConn {
return YES;
}
+ (NSString *)shortHelpText {
return @"Get status about Santa";
}

View File

@@ -42,6 +42,10 @@ REGISTER_COMMAND_NAME(@"sync");
return NO;
}
+ (BOOL)requiresDaemonConn {
return YES;
}
+ (NSString *)shortHelpText {
return @"Synchronizes Santa with the server";
}