Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
theevilbit committed Sep 22, 2020
0 parents commit 3f892fa
Show file tree
Hide file tree
Showing 68 changed files with 5,333 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
23 changes: 23 additions & 0 deletions Common/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Constants.h
// ShieldProject
//
// Created by csaby on 2020. 06. 11..
// Copyright © 2020. csaba.fitzl. All rights reserved.
//

#ifndef Constants_h
#define Constants_h

#define TEAM_ID @"33YRLYRBYV"
#define MAIN_APP_ID @"com.csaba.fitzl.shield"
#define PREFS_FILE @"com.csaba.fitzl.shield.preferences.plist"
#define LOG_FILE_NAME @"shield.log"
#define DIR_PATH_ES @"/Library/Application Support/Shield"

//log to file flag
#define LOG_TO_FILE 0x10

#define LOG_ROOT 0x0

#endif /* Constants_h */
30 changes: 30 additions & 0 deletions Common/XPCProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// XPCProtocol.h
// ShieldProject
//
// Created by csaby on 2020. 06. 08..
// Copyright © 2020. csaba.fitzl. All rights reserved.
//

#ifndef XPCProtocol_h
#define XPCProtocol_h
@protocol ProviderCommunication

//start ES client
-(void)startWithReply:(void (^)(BOOL))reply;
//stop ES client
-(void)stopWithReply:(void (^)(BOOL))reply;
//register ES client
-(void)registerWithReply:(void (^)(BOOL))reply;
-(void)getStatus:(void (^)(NSDictionary *))reply;
-(void)updatePrefs:(NSDictionary *)prefs;

@end

@protocol AppCommunication
//notify app
-(void)notify:(NSString *)notification blocked:(BOOL)blockStatus;
@end


#endif /* XPCProtocol_h */
36 changes: 36 additions & 0 deletions Common/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// file: logging.h
// project: BlockBlock (shared)
// description: logging functions (header)
//
// created by Patrick Wardle
// copyright (c) 2017 Objective-See. All rights reserved.
//

#ifndef Logging_h
#define Logging_h

#import <syslog.h>

@import Cocoa;
@import Foundation;

//log a msg to syslog
// also disk, if error
void logMsg(int level, NSString* msg);

//prep/open log file
BOOL initLogging(NSString* logPath);

//get path to log file
NSString* logFilePath(int user);

//de-init logging
void deinitLogging(void);

//log to file
void log2File(NSString* msg);

void setLoggingUser(int i);

#endif
201 changes: 201 additions & 0 deletions Common/logging.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//
// file: logging.m
// project: BlockBlock (shared)
// description: logging functions
//
// created by Patrick Wardle
// copyright (c) 2017 Objective-See. All rights reserved.
//

#import "Constants.h"
#import "logging.h"

//global log file handle
NSFileHandle* logFileHandle = nil;
int logging_user = 0;

void setLoggingUser(int i) {
logging_user = i;
}

//get path to log file
NSString* logFilePath()
{
if(logging_user == LOG_ROOT) {
return [DIR_PATH_ES stringByAppendingPathComponent:LOG_FILE_NAME];
}
else {
return [NSHomeDirectory() stringByAppendingPathComponent:[DIR_PATH_ES stringByAppendingPathComponent:LOG_FILE_NAME]];
}
}


//log a msg
// default to syslog, and if an err msg, to disk
void logMsg(int level, NSString* msg)
{
//flag for logging
BOOL shouldLog = NO;

//log prefix
NSMutableString* logPrefix = nil;

//first grab logging flag
shouldLog = (LOG_TO_FILE == (level & LOG_TO_FILE));

//then remove it
// make sure syslog is happy
level &= ~LOG_TO_FILE;

//alloc/init
// always start w/ name + pid
logPrefix = [NSMutableString stringWithFormat:@"%@(%d)", @"Shield", getpid()];

//if its error, add error to prefix
if(LOG_ERR == level)
{
//add
[logPrefix appendString:@" ERROR"];
}

//debug mode logic
#ifdef DEBUG

//in debug mode promote debug msgs to LOG_NOTICE
// OS X only shows LOG_NOTICE and above
if(LOG_DEBUG == level)
{
//promote
level = LOG_NOTICE;
}

#endif

//dump to syslog?
// function can be invoked just to log to file...
if(0 != level)
{
//syslog
syslog(level, "%s: %s", [logPrefix UTF8String], [msg UTF8String]);
}

//when a message is to be logged to file
// log it, when logging is enabled
if(YES == shouldLog)
{
//but only when logging is enable
if(nil != logFileHandle)
{
//log
log2File(msg);
}
}

return;
}

//log to file
void log2File(NSString* msg)
{
//sync
@synchronized(logFileHandle)
{
//append timestamp
// write msg out to disk
[logFileHandle writeData:[[NSString stringWithFormat:@"%@: %@\n", [NSDate date], msg] dataUsingEncoding:NSUTF8StringEncoding]];
}

return;
}

//de-init logging
void deinitLogging()
{
//dbg msg
// ->and to file
logMsg(LOG_DEBUG|LOG_TO_FILE, @"logging ending");

//sync
@synchronized(logFileHandle)
{
//close file handle
[logFileHandle closeFile];

//unset
logFileHandle = nil;
}

return;
}

//prep/open log file
BOOL initLogging(NSString* logPath)
{
//ret var
BOOL bRet = NO;

//first time
//check if dir exists
BOOL isDir = NO;
NSString* dir = @"";
if(logging_user == LOG_ROOT) {
dir = DIR_PATH_ES;
}
else {
dir = [NSHomeDirectory() stringByAppendingPathComponent:DIR_PATH_ES];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDir] != YES)
{
NSError * error = nil;

[[NSFileManager defaultManager] createDirectoryAtPath:dir
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error != nil) {
logMsg(LOG_ERR, [NSString stringWithFormat:@"Failed creating directory: %@", error]);
goto bail;
}
}
// create file
if(YES != [[NSFileManager defaultManager] fileExistsAtPath:logPath])
{
//create
if(YES != [[NSFileManager defaultManager] createFileAtPath:logPath contents:nil attributes:nil])
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to create log file, %@", logPath]);

//bail
goto bail;
}
}

//get file handle
logFileHandle = [NSFileHandle fileHandleForWritingAtPath:logPath];
if(nil == logFileHandle)
{
//err msg
logMsg(LOG_ERR, [NSString stringWithFormat:@"failed to get log file handle to %@", logPath]);

//bail
goto bail;
}

//dbg msg
logMsg(LOG_DEBUG, [NSString stringWithFormat:@"opened log file; %@", logPath]);

//seek to end
[logFileHandle seekToEndOfFile];

//dbg msg
// ->and to file
logMsg(LOG_DEBUG|LOG_TO_FILE, @"logging intialized");

//happy
bRet = YES;

bail:

return bRet;
}
12 changes: 12 additions & 0 deletions Extension/Extension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.endpoint-security.client</key>
<true/>
<key>com.apple.developer.system-extension.install</key>
<true/>
<key>com.apple.security.automation.apple-events</key>
<true/>
</dict>
</plist>
35 changes: 35 additions & 0 deletions Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Shield System Extension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>SYSX</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>EndpointExtension</key>
<dict>
<key>MachServiceName</key>
<string>$(TeamIdentifierPrefix)com.csaba.fitzl.shield.Extension.xpc</string>
</dict>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020. Csaba Fitzl. All rights reserved.</string>
<key>NSSystemExtensionUsageDescription</key>
<string></string>
</dict>
</plist>
36 changes: 36 additions & 0 deletions Extension/Preferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Preferences.h
// Shield System Extension
//
// Created by csaby on 2020. 06. 11..
// Copyright © 2020. csaba.fitzl. All rights reserved.
//

#ifndef Preferences_h
#define Preferences_h

//
// Preferences.h
// Daemon
//
// Created by Patrick Wardle on 2/22/18.
// Copyright © 2018 Objective-See. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Preferences : NSObject

/* PROPERTIES */

//preferences
@property(nonatomic, retain)NSMutableDictionary* preferences;

/* METHODS */

//load/save prefs from disk
-(BOOL)load;
-(BOOL)save;

@end
#endif /* Preferences_h */
Loading

0 comments on commit 3f892fa

Please sign in to comment.