2016-09-12 01:21:14 +01:00
|
|
|
//
|
|
|
|
// Configure.m
|
|
|
|
// OverSight
|
|
|
|
//
|
|
|
|
// Created by Patrick Wardle on 9/01/16.
|
|
|
|
// Copyright (c) 2016 Objective-See. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
2016-10-09 06:34:31 +01:00
|
|
|
#import <ServiceManagement/ServiceManagement.h>
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
#import "Consts.h"
|
|
|
|
#import "Logging.h"
|
|
|
|
#import "Utilities.h"
|
|
|
|
#import "Configure.h"
|
|
|
|
|
|
|
|
@implementation Configure
|
|
|
|
|
|
|
|
//invokes appropriate install || uninstall logic
|
|
|
|
-(BOOL)configure:(NSUInteger)parameter
|
|
|
|
{
|
|
|
|
//return var
|
|
|
|
BOOL wasConfigured = NO;
|
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//install
|
|
|
|
// ->starts on success
|
2016-09-12 01:21:14 +01:00
|
|
|
if(ACTION_INSTALL_FLAG == parameter)
|
|
|
|
{
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"installing...");
|
|
|
|
|
|
|
|
//if already installed though
|
|
|
|
// ->uninstall everything first
|
|
|
|
if(YES == [self isInstalled])
|
|
|
|
{
|
|
|
|
//dbg msg
|
2016-09-18 22:10:44 +01:00
|
|
|
logMsg(LOG_DEBUG, @"already installed, so stopping/uninstalling...");
|
|
|
|
|
|
|
|
//stop
|
|
|
|
// ->kill login item/XPC service
|
|
|
|
[self stop];
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//uninstall
|
|
|
|
if(YES != [self uninstall])
|
|
|
|
{
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"uninstalled");
|
|
|
|
}
|
|
|
|
|
|
|
|
//install
|
|
|
|
if(YES != [self install])
|
|
|
|
{
|
2016-09-16 09:13:06 +01:00
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, @"installation failed");
|
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//dbg msg
|
2016-09-16 09:13:06 +01:00
|
|
|
logMsg(LOG_DEBUG, @"installed, now will start");
|
2016-09-25 04:08:38 +01:00
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//start login item
|
|
|
|
if(YES != [self start])
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, @"starting failed");
|
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
2016-09-12 01:21:14 +01:00
|
|
|
}
|
2016-09-16 09:13:06 +01:00
|
|
|
//uninstall
|
|
|
|
// ->stops login item (also w/ stop XPC service)
|
2016-09-12 01:21:14 +01:00
|
|
|
else if(ACTION_UNINSTALL_FLAG == parameter)
|
|
|
|
{
|
2016-09-16 09:13:06 +01:00
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"stopping login item");
|
|
|
|
|
2016-09-18 22:10:44 +01:00
|
|
|
//stop
|
|
|
|
// ->kill login item/XPC service
|
|
|
|
[self stop];
|
2016-09-16 09:13:06 +01:00
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"uninstalling...");
|
|
|
|
|
|
|
|
//uninstall
|
|
|
|
if(YES != [self uninstall])
|
|
|
|
{
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"uninstalled!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//no errors
|
|
|
|
wasConfigured = YES;
|
|
|
|
|
|
|
|
//bail
|
|
|
|
bail:
|
|
|
|
|
|
|
|
return wasConfigured;
|
|
|
|
}
|
|
|
|
|
|
|
|
//determine if installed
|
2016-09-16 09:13:06 +01:00
|
|
|
// ->simply checks if application exists in /Applications
|
2016-09-12 01:21:14 +01:00
|
|
|
-(BOOL)isInstalled
|
|
|
|
{
|
|
|
|
//check if extension exists
|
2016-09-15 09:15:30 +01:00
|
|
|
return [[NSFileManager defaultManager] fileExistsAtPath:[APPS_FOLDER stringByAppendingPathComponent:APP_NAME]];
|
2016-09-12 01:21:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//install
|
2016-09-15 09:15:30 +01:00
|
|
|
// a) copy to /Applications
|
|
|
|
// b) chown/chmod XPC component
|
2016-09-12 01:21:14 +01:00
|
|
|
-(BOOL)install
|
|
|
|
{
|
|
|
|
//return/status var
|
|
|
|
BOOL wasInstalled = NO;
|
|
|
|
|
|
|
|
//error
|
|
|
|
NSError* error = nil;
|
|
|
|
|
2016-09-15 09:15:30 +01:00
|
|
|
//path to app (src)
|
|
|
|
NSString* appPathSrc = nil;
|
2016-09-12 01:21:14 +01:00
|
|
|
|
2016-09-15 09:15:30 +01:00
|
|
|
//path to app (dest)
|
|
|
|
NSString* appPathDest = nil;
|
2016-09-12 01:21:14 +01:00
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//path to XPC service
|
|
|
|
NSString* xpcServicePath = nil;
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//path to login item
|
|
|
|
NSString* loginItem = nil;
|
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//set src path
|
|
|
|
// ->orginally stored in installer app's /Resource bundle
|
2016-09-15 09:15:30 +01:00
|
|
|
appPathSrc = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:APP_NAME];
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//set dest path
|
2016-09-15 09:15:30 +01:00
|
|
|
appPathDest = [APPS_FOLDER stringByAppendingPathComponent:APP_NAME];
|
2016-09-12 01:21:14 +01:00
|
|
|
|
2016-09-15 09:15:30 +01:00
|
|
|
//move app into /Applications
|
|
|
|
if(YES != [[NSFileManager defaultManager] copyItemAtPath:appPathSrc toPath:appPathDest error:&error])
|
2016-09-12 01:21:14 +01:00
|
|
|
{
|
|
|
|
//err msg
|
2016-09-15 09:15:30 +01:00
|
|
|
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to copy %@ -> %@ (%@)", appPathSrc, appPathDest, error]);
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//dbg msg
|
2016-09-15 09:15:30 +01:00
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"copied %@ -> %@", appPathSrc, appPathDest]);
|
2016-09-16 09:13:06 +01:00
|
|
|
|
2016-10-05 05:03:41 +01:00
|
|
|
//remove xattrs
|
|
|
|
// ->otherwise app translocation causes issues
|
|
|
|
execTask(XATTR, @[@"-cr", appPathDest]);
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"removed xattrz");
|
|
|
|
|
|
|
|
//init path to login item
|
|
|
|
loginItem = [appPathDest stringByAppendingPathComponent:@"Contents/Library/LoginItems/OverSight Helper.app/Contents/MacOS/OverSight Helper"];
|
|
|
|
|
|
|
|
//call into login item to install itself
|
|
|
|
// ->runs as non-root, so can access user's login items, etc
|
|
|
|
execTask(loginItem, @[ACTION_INSTALL]);
|
|
|
|
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"persisted %@", loginItem]);
|
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//init path to XPC service
|
|
|
|
xpcServicePath = [appPathDest stringByAppendingPathComponent:@"Contents/Library/LoginItems/OverSight Helper.app/Contents/XPCServices/OverSightXPC.xpc"];
|
2016-09-12 01:21:14 +01:00
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//set XPC service to be owned; root:wheel
|
|
|
|
if(YES != setFileOwner(xpcServicePath, @0, @0, YES))
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to set file owner to root:wheel on %@", xpcServicePath]);
|
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set XPC service binary to setuid
|
|
|
|
if(YES != setFilePermissions(xpcServicePath, 06755, YES))
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to set file permissions to 06755 on %@", xpcServicePath]);
|
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
2016-09-15 09:15:30 +01:00
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//no error
|
|
|
|
wasInstalled = YES;
|
|
|
|
|
|
|
|
//bail
|
|
|
|
bail:
|
|
|
|
|
|
|
|
return wasInstalled;
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//start
|
|
|
|
// ->just the login item
|
|
|
|
-(BOOL)start
|
|
|
|
{
|
|
|
|
//flag
|
|
|
|
BOOL bStarted = NO;
|
|
|
|
|
|
|
|
//path to login item
|
|
|
|
NSString* loginItem = nil;
|
|
|
|
|
2016-09-25 04:08:38 +01:00
|
|
|
//task
|
|
|
|
NSTask* task = nil;
|
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//init path
|
2016-09-25 04:08:38 +01:00
|
|
|
loginItem = [[APPS_FOLDER stringByAppendingPathComponent:APP_NAME] stringByAppendingPathComponent:@"Contents/Library/LoginItems/OverSight Helper.app/Contents/MacOS/OverSight Helper"];
|
|
|
|
|
|
|
|
//alloc task
|
|
|
|
task = [[NSTask alloc] init];
|
|
|
|
|
|
|
|
//set path
|
|
|
|
[task setLaunchPath:loginItem];
|
|
|
|
|
|
|
|
//wrap task launch
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
//launch
|
|
|
|
[task launch];
|
|
|
|
}
|
|
|
|
@catch(NSException* exception)
|
2016-09-16 09:13:06 +01:00
|
|
|
{
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
2016-09-25 04:08:38 +01:00
|
|
|
|
2016-09-16 09:13:06 +01:00
|
|
|
//happy
|
|
|
|
bStarted = YES;
|
|
|
|
|
|
|
|
//bail
|
|
|
|
bail:
|
|
|
|
|
|
|
|
return bStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
//stop
|
2016-09-18 22:10:44 +01:00
|
|
|
-(void)stop
|
2016-09-16 09:13:06 +01:00
|
|
|
{
|
|
|
|
//kill it
|
|
|
|
// pkill doesn't provide error info, so...
|
|
|
|
execTask(PKILL, @[APP_HELPER_NAME]);
|
2016-09-18 22:10:44 +01:00
|
|
|
|
|
|
|
return;
|
2016-09-16 09:13:06 +01:00
|
|
|
}
|
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//uninstall
|
2016-09-16 09:13:06 +01:00
|
|
|
// ->delete app
|
2016-09-12 01:21:14 +01:00
|
|
|
-(BOOL)uninstall
|
|
|
|
{
|
|
|
|
//return/status var
|
|
|
|
BOOL wasUninstalled = NO;
|
|
|
|
|
|
|
|
//status var
|
|
|
|
// ->since want to try all uninstall steps, but record if any fail
|
|
|
|
BOOL bAnyErrors = NO;
|
|
|
|
|
|
|
|
//path to finder sync
|
2016-09-15 09:15:30 +01:00
|
|
|
NSString* appPath = nil;
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//error
|
|
|
|
NSError* error = nil;
|
2016-10-09 06:34:31 +01:00
|
|
|
|
|
|
|
//path to login item
|
|
|
|
NSString* loginItem = nil;
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//init path
|
2016-09-15 09:15:30 +01:00
|
|
|
appPath = [APPS_FOLDER stringByAppendingPathComponent:APP_NAME];
|
2016-10-09 06:34:31 +01:00
|
|
|
|
|
|
|
//init path to login item
|
|
|
|
loginItem = [appPath stringByAppendingPathComponent:@"Contents/Library/LoginItems/OverSight Helper.app/Contents/MacOS/OverSight Helper"];
|
|
|
|
|
|
|
|
//call into login item to install itself
|
|
|
|
// ->runs as non-root, so can access user's login items, etc
|
|
|
|
execTask(loginItem, @[ACTION_UNINSTALL]);
|
|
|
|
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"unpersisted %@", loginItem]);
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//delete folder
|
2016-09-15 09:15:30 +01:00
|
|
|
if(YES != [[NSFileManager defaultManager] removeItemAtPath:appPath error:&error])
|
2016-09-12 01:21:14 +01:00
|
|
|
{
|
|
|
|
//err msg
|
2016-09-15 09:15:30 +01:00
|
|
|
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to delete app %@ (%@)", appPath, error]);
|
2016-09-12 01:21:14 +01:00
|
|
|
|
|
|
|
//set flag
|
|
|
|
bAnyErrors = YES;
|
|
|
|
|
|
|
|
//keep uninstalling...
|
|
|
|
}
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
//only success when there were no errors
|
|
|
|
if(YES != bAnyErrors)
|
|
|
|
{
|
|
|
|
//happy
|
|
|
|
wasUninstalled = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasUninstalled;
|
|
|
|
}
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
|
2016-09-12 01:21:14 +01:00
|
|
|
@end
|
|
|
|
|