Skip to content

Commit

Permalink
Add new install command
Browse files Browse the repository at this point in the history
  • Loading branch information
mlw committed Oct 16, 2024
1 parent 733929b commit a737dc7
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 28 deletions.
18 changes: 12 additions & 6 deletions Source/common/SNTXPCControlInterface.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/// Copyright 2015 Google Inc. All rights reserved.
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

#import "Source/common/SNTRuleIdentifiers.h"
#import "Source/common/SNTXPCUnprivilegedControlInterface.h"
Expand Down Expand Up @@ -59,6 +60,11 @@
///
- (void)postRuleSyncNotificationWithCustomMessage:(NSString *)message reply:(void (^)(void))reply;

///
/// Control Ops
///
- (void)installSantaApp:(NSString*)appPath reply:(void (^)(BOOL))reply;

@end

@interface SNTXPCControlInterface : NSObject
Expand Down
12 changes: 12 additions & 0 deletions Source/santactl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ objc_library(
],
)

objc_library(
name = "SNTCommandInstall",
srcs = ["Commands/SNTCommandInstall.mm"],
deps = [
":santactl_cmd",
"//Source/common:SNTLogging",
"//Source/common:SNTXPCControlInterface",
"@MOLXPCConnection",
],
)

objc_library(
name = "santactl_lib",
srcs = [
Expand All @@ -58,6 +69,7 @@ objc_library(
sdk_dylibs = ["libz"],
sdk_frameworks = ["IOKit"],
deps = [
":SNTCommandInstall",
":SNTCommandPrintLog",
":santactl_cmd",
"//Source/common:SNTCachedDecision",
Expand Down
87 changes: 87 additions & 0 deletions Source/santactl/Commands/SNTCommandInstall.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

#import <Foundation/Foundation.h>
#import <MOLXPCConnection/MOLXPCConnection.h>

#include "Source/common/SNTLogging.h"
#import "Source/common/SNTXPCControlInterface.h"
#import "Source/santactl/SNTCommand.h"
#import "Source/santactl/SNTCommandController.h"

@interface SNTCommandInstall : SNTCommand <SNTCommandProtocol>
@end

@implementation SNTCommandInstall

REGISTER_COMMAND_NAME(@"install")

+ (BOOL)requiresRoot {
return YES;
}

+ (BOOL)requiresDaemonConn {
return YES;
}

+ (NSString *)shortHelpText {
return @"Install a given package";
}

+ (NSString *)longHelpText {
return @"Instruct the daemon to install Santa.app.\n"
@"\n"
@" --path {path}: Path to the Santa.app bundle to install.\n"
@"\n";
}

+ (BOOL)isHidden {
return YES;
}

- (void)runWithArguments:(NSArray *)arguments {
NSString *path;

for (NSUInteger i = 0; i < arguments.count; ++i) {
NSString *arg = arguments[i];

if ([arg caseInsensitiveCompare:@"--path"] == NSOrderedSame) {
if (++i > arguments.count - 1) {
[self printErrorUsageAndExit:@"--path requires an argument"];
}
path = arguments[i];
}
}

if (!path) {
[self printErrorUsageAndExit:@"No path specified"];
}

LOGI(@"Asking daemon to install: %@", path);

dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[[self.daemonConn remoteObjectProxy] installSantaApp:(NSString*)path reply:^(BOOL success){
LOGI(@"Got reply from daemon: %d", success);
dispatch_semaphore_signal(sema);
}];


if (dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC)) > 0) {
LOGW(@"Timed out waiting for install to complete.");
}

exit(EXIT_SUCCESS);
}

@end
24 changes: 16 additions & 8 deletions Source/santactl/SNTCommand.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/// Copyright 2017 Google Inc. All rights reserved.
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

#import <Foundation/Foundation.h>

@class MOLXPCConnection;

@protocol SNTCommandProtocol
@protocol SNTCommandProtocol <NSObject>

///
/// @return YES if command requires root.
Expand All @@ -38,9 +39,16 @@
///
+ (NSString *)longHelpText;

@optional

///
/// YES if the command should be hidden from usage text.
///
+ (BOOL)isHidden;

@end

@protocol SNTCommandRunProtocol
@protocol SNTCommandRunProtocol <NSObject>

///
/// Called when the user is running the command
Expand Down
22 changes: 14 additions & 8 deletions Source/santactl/SNTCommandController.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/// Copyright 2015 Google Inc. All rights reserved.
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

#import "Source/santactl/SNTCommandController.h"

Expand Down Expand Up @@ -46,8 +47,13 @@ + (NSString *)usage {
for (NSString *cmdName in
[[registeredCommands allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]) {
Class<SNTCommandProtocol> command = registeredCommands[cmdName];
[helpText appendFormat:@"\t%*s - %@\n", longestCommandName, [cmdName UTF8String],
[command shortHelpText]];

BOOL hidden = [command respondsToSelector:@selector(isHidden)] && [command isHidden];

if (!hidden) {
[helpText appendFormat:@"\t%*s - %@\n", longestCommandName, [cmdName UTF8String],
[command shortHelpText]];
}
}

[helpText appendFormat:@"\nSee 'santactl help <command>' to read about a specific subcommand."];
Expand Down
20 changes: 14 additions & 6 deletions Source/santad/SNTDaemonControlController.mm
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/// Copyright 2015-2022 Google Inc. All rights reserved.
/// Copyright 2024 North Pole Security, Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

#import "Source/santad/SNTDaemonControlController.h"

Expand Down Expand Up @@ -384,4 +385,11 @@ - (void)syncBundleEvent:(SNTStoredEvent *)event relatedEvents:(NSArray<SNTStored
}];
}

#pragma mark Control Ops

- (void)installSantaApp:(NSString*)appPath reply:(void (^)(BOOL))reply {
LOGI(@"Got to the install path: %@", appPath);
reply(YES);
}

@end

0 comments on commit a737dc7

Please sign in to comment.