2016-09-12 01:21:14 +01:00
|
|
|
//
|
|
|
|
// main.m
|
|
|
|
// Test Application Helper
|
|
|
|
//
|
|
|
|
// Created by Patrick Wardle on 9/10/16.
|
|
|
|
// Copyright (c) 2016 Objective-See. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
#import "main.h"
|
2016-11-14 19:12:19 +00:00
|
|
|
#import "Logging.h"
|
2016-11-08 07:36:03 +00:00
|
|
|
#import "Utilities.h"
|
2017-03-28 09:00:11 +01:00
|
|
|
#import "../Shared/XPCProtocol.h"
|
2016-09-12 01:21:14 +01:00
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//go go go
|
|
|
|
// ->either install/uninstall, or just launch normally
|
|
|
|
int main(int argc, const char * argv[])
|
|
|
|
{
|
|
|
|
//return var
|
|
|
|
int iReturn = 0;
|
|
|
|
|
2017-09-25 07:45:02 +01:00
|
|
|
//logged in user info
|
|
|
|
NSMutableDictionary* userInfo = nil;
|
|
|
|
|
|
|
|
//pool
|
|
|
|
@autoreleasepool
|
|
|
|
{
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2017-09-25 07:45:02 +01:00
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"starting login item (args: %@/user: %@/%@)", [[NSProcessInfo processInfo] arguments], NSUserName(), loggedinUser()]);
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-28 09:00:11 +01:00
|
|
|
//check for uninstall/install flags, and process to remove from whitelist
|
2016-10-09 06:34:31 +01:00
|
|
|
if(2 == argc)
|
|
|
|
{
|
2017-09-25 07:45:02 +01:00
|
|
|
//drops privs when installing/uninstalling
|
|
|
|
// do here, only for these as they then bail
|
|
|
|
if( (0 == strcmp(argv[1], CMD_INSTALL)) ||
|
|
|
|
(0 == strcmp(argv[1], CMD_UNINSTALL)) )
|
|
|
|
{
|
|
|
|
//get user
|
|
|
|
userInfo = loggedinUser();
|
|
|
|
if(nil == userInfo[@"user"])
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, @"failed to determine logged-in user");
|
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//drop group privs
|
|
|
|
setgid([userInfo[@"gid"] intValue]);
|
|
|
|
|
|
|
|
//drop user privs
|
|
|
|
setuid([userInfo[@"uid"] intValue]);
|
|
|
|
}
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//install
|
2017-03-28 09:00:11 +01:00
|
|
|
if(0 == strcmp(argv[1], CMD_INSTALL))
|
2016-10-09 06:34:31 +01:00
|
|
|
{
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2016-11-14 19:12:19 +00:00
|
|
|
logMsg(LOG_DEBUG, @"running install logic");
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//install
|
2017-04-14 10:24:01 +01:00
|
|
|
if(YES != toggleLoginItem([NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]], ACTION_INSTALL_FLAG))
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, @"failed to add login item");
|
|
|
|
|
|
|
|
//set error
|
|
|
|
iReturn = -1;
|
|
|
|
|
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
2016-11-08 07:36:03 +00:00
|
|
|
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2016-11-14 19:12:19 +00:00
|
|
|
logMsg(LOG_DEBUG, @"installed login item");
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2016-11-08 07:36:03 +00:00
|
|
|
//create default prefs
|
|
|
|
[@{PREF_LOG_ACTIVITY:@YES, PREF_START_AT_LOGIN:@YES, PREF_RUN_HEADLESS:@NO, PREF_CHECK_4_UPDATES:@YES} writeToFile:[APP_PREFERENCES stringByExpandingTildeInPath] atomically:NO];
|
2016-10-09 06:34:31 +01:00
|
|
|
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2016-11-14 19:12:19 +00:00
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"created preferences at: %@", [APP_PREFERENCES stringByExpandingTildeInPath]]);
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
//uninstall
|
2017-03-28 09:00:11 +01:00
|
|
|
else if(0 == strcmp(argv[1], CMD_UNINSTALL))
|
2016-10-09 06:34:31 +01:00
|
|
|
{
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2016-11-14 19:12:19 +00:00
|
|
|
logMsg(LOG_DEBUG, @"running uninstall logic");
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2017-04-13 10:29:18 +01:00
|
|
|
//uninstall
|
2017-04-14 10:24:01 +01:00
|
|
|
if(YES != toggleLoginItem([NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]], ACTION_UNINSTALL_FLAG))
|
2017-04-13 10:29:18 +01:00
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, @"failed to remove login item");
|
|
|
|
|
|
|
|
//set error
|
|
|
|
iReturn = -1;
|
|
|
|
|
2017-04-14 10:24:01 +01:00
|
|
|
//don't bail
|
|
|
|
// ->keep trying to uninstall
|
2017-04-13 10:29:18 +01:00
|
|
|
}
|
2017-04-14 10:24:01 +01:00
|
|
|
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2017-04-14 10:24:01 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//dbg msg
|
|
|
|
logMsg(LOG_DEBUG, @"removed login item");
|
|
|
|
}
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2016-11-08 07:36:03 +00:00
|
|
|
//delete prefs
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:[APP_PREFERENCES stringByExpandingTildeInPath] error:nil];
|
2016-10-09 06:34:31 +01:00
|
|
|
|
2016-11-14 19:12:19 +00:00
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2016-11-14 19:12:19 +00:00
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"removed preferences from: %@", [APP_PREFERENCES stringByExpandingTildeInPath]]);
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2016-11-14 19:12:19 +00:00
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
//bail
|
|
|
|
goto bail;
|
|
|
|
}
|
2017-04-08 08:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//unwhitelist path/device
|
|
|
|
else if(3 == argc)
|
|
|
|
{
|
|
|
|
//dbg msg
|
|
|
|
#ifdef DEBUG
|
|
|
|
logMsg(LOG_DEBUG, @"running 'un-whitelist me' logic");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//remove from whitelist file
|
|
|
|
unWhiteList([NSString stringWithUTF8String:argv[1]], [NSNumber numberWithInt:atoi(argv[2])]);
|
2017-03-28 09:00:11 +01:00
|
|
|
|
2017-04-08 08:37:23 +01:00
|
|
|
//don't bail
|
|
|
|
// ->let it start (as it was killed)
|
2016-10-09 06:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//launch app normally
|
|
|
|
iReturn = NSApplicationMain(argc, argv);
|
|
|
|
|
2017-09-25 07:45:02 +01:00
|
|
|
}//pool
|
|
|
|
|
2016-10-09 06:34:31 +01:00
|
|
|
bail:
|
|
|
|
|
|
|
|
return iReturn;
|
|
|
|
}
|
2017-03-28 09:00:11 +01:00
|
|
|
|
|
|
|
//send XPC message to remove process from whitelist file
|
2017-04-08 08:37:23 +01:00
|
|
|
void unWhiteList(NSString* process, NSNumber* device)
|
2017-03-28 09:00:11 +01:00
|
|
|
{
|
|
|
|
//xpc connection
|
|
|
|
__block NSXPCConnection* xpcConnection = nil;
|
|
|
|
|
|
|
|
//init XPC
|
|
|
|
xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.objective-see.OverSightXPC"];
|
|
|
|
|
|
|
|
//set remote object interface
|
|
|
|
xpcConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCProtocol)];
|
|
|
|
|
|
|
|
//resume
|
|
|
|
[xpcConnection resume];
|
|
|
|
|
|
|
|
//dbg msg
|
2017-04-03 00:12:42 +01:00
|
|
|
#ifdef DEBUG
|
2017-04-08 08:37:23 +01:00
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"sending XPC message to remove %@/%@ from whitelist file", process, device]);
|
2017-04-03 00:12:42 +01:00
|
|
|
#endif
|
2017-03-28 09:00:11 +01:00
|
|
|
|
|
|
|
//invoke XPC method 'whitelistProcess' to add process to white list
|
2017-04-08 08:37:23 +01:00
|
|
|
[[xpcConnection remoteObjectProxy] unWhitelistProcess:process device:device reply:^(BOOL wasRemoved)
|
2017-03-28 09:00:11 +01:00
|
|
|
{
|
2017-04-03 00:12:42 +01:00
|
|
|
//dbg msg
|
|
|
|
#ifdef DEBUG
|
|
|
|
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"got XPC response: %d", wasRemoved]);
|
|
|
|
#endif
|
2017-03-28 09:00:11 +01:00
|
|
|
|
2017-04-03 00:12:42 +01:00
|
|
|
//err msg on failure
|
|
|
|
if(YES != wasRemoved)
|
|
|
|
{
|
|
|
|
//err msg
|
|
|
|
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to remove %@ from whitelist", process]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//close connection
|
|
|
|
[xpcConnection invalidate];
|
2017-03-28 09:00:11 +01:00
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|