mirror of
https://github.com/google/santa.git
synced 2026-04-24 03:00:12 -04:00
santactl: Simplify protocol and daemonConn handling
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -29,6 +29,10 @@ REGISTER_COMMAND_NAME(@"flushcache");
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (BOOL)requiresDaemonConn {
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (NSString *)shortHelpText {
|
||||
return @"Flush the kernel cache";
|
||||
}
|
||||
|
||||
@@ -75,6 +75,6 @@ int main(int argc, const char *argv[]) {
|
||||
return 128;
|
||||
}
|
||||
|
||||
return [SNTCommandController runCommandWithName:commandName arguments:arguments];
|
||||
[SNTCommandController runCommandWithName:commandName arguments:arguments];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,11 @@
|
||||
REGISTER_COMMAND_NAME(@"rule");
|
||||
|
||||
+ (BOOL)requiresRoot {
|
||||
return TRUE;
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (BOOL)requiresDaemonConn {
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (NSString *)shortHelpText {
|
||||
|
||||
@@ -33,6 +33,10 @@ REGISTER_COMMAND_NAME(@"status");
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (BOOL)requiresDaemonConn {
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (NSString *)shortHelpText {
|
||||
return @"Get status about Santa";
|
||||
}
|
||||
|
||||
@@ -42,6 +42,10 @@ REGISTER_COMMAND_NAME(@"sync");
|
||||
return NO;
|
||||
}
|
||||
|
||||
+ (BOOL)requiresDaemonConn {
|
||||
return YES;
|
||||
}
|
||||
|
||||
+ (NSString *)shortHelpText {
|
||||
return @"Synchronizes Santa with the server";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user