Skip to content

Commit

Permalink
Tweaks to reduce permablocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Stigler committed Oct 24, 2009
1 parent b90f1c6 commit 716f8fa
Show file tree
Hide file tree
Showing 186 changed files with 905 additions and 1,965 deletions.
12 changes: 6 additions & 6 deletions DomainListWindowController.m
Expand Up @@ -81,7 +81,7 @@ - (IBAction)removeDomain:(id)sender
[domainListTableView_ reloadData];

[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
Expand Down Expand Up @@ -178,7 +178,7 @@ - (void)tableView:(NSTableView *)aTableView
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[aTableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}

/* - (NSCell *)tableView:(NSTableView*)tableView
Expand Down Expand Up @@ -289,7 +289,7 @@ - (IBAction)importIncomingMailServersFromThunderbird:(id)sender {
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}

- (IBAction)importOutgoingMailServersFromThunderbird:(id)sender {
Expand All @@ -302,7 +302,7 @@ - (IBAction)importOutgoingMailServersFromThunderbird:(id)sender {
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}


Expand All @@ -316,7 +316,7 @@ - (IBAction)importIncomingMailServersFromMail:(id)sender {
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}


Expand All @@ -330,7 +330,7 @@ - (IBAction)importOutgoingMailServersFromMail:(id)sender {
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: nil];
object: self];
}

- (void)dealloc {
Expand Down
Empty file modified ERRORS 100644 → 100755
Empty file.
129 changes: 73 additions & 56 deletions HelperMain.m
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) {
[NSNumber numberWithUnsignedLong: 0], NSFileGroupOwnerAccountID,
// 493 (decimal) = 755 (octal) = rwxr-xr-x
[NSNumber numberWithUnsignedLong: 493], NSFilePosixPermissions,
nil];
nil];

// This is where we get going with the lockfile system, saving a "lock" in /etc/SelfControl.lock
// to make a more reliable block detection system. For most of the program,
Expand Down Expand Up @@ -328,8 +328,7 @@ int main(int argc, char* argv[]) {
NSDate* blockStartedDate = [curDictionary objectForKey: @"BlockStartedDate"];
NSTimeInterval blockDuration = [[curDictionary objectForKey: @"BlockDuration"] intValue];

if(blockStartedDate == nil || [blockStartedDate isEqualToDate: [NSDate distantFuture]]
|| blockDuration < 1) {
if(blockStartedDate == nil || [[NSDate distantFuture] isEqualToDate: blockStartedDate] || blockDuration < 1) {
// The lock file seems to be broken. Read from defaults, then write out a
// new lock file while we're at it.
[NSUserDefaults resetStandardUserDefaults];
Expand All @@ -342,10 +341,9 @@ int main(int argc, char* argv[]) {
[NSUserDefaults resetStandardUserDefaults];
seteuid(0);

if(blockStartedDate == nil || [blockStartedDate isEqualToDate: [NSDate distantFuture]]
|| blockDuration < 1) {
if(blockStartedDate == nil || blockDuration < 1) {
// Defaults is broken too! Let's get out of here!
NSLog(@"ERROR: Checkup ran -- no block found.");
NSLog(@"ERROR: Checkup ran but no block found. This may leave a block permanently applied.");
printStatus(-215);
exit(EX_SOFTWARE);
}
Expand Down Expand Up @@ -383,7 +381,6 @@ int main(int argc, char* argv[]) {
if([[NSFileManager defaultManager] isDeletableFileAtPath: kSelfControlLockFilePath] && ![[NSFileManager defaultManager] removeFileAtPath: kSelfControlLockFilePath handler: nil]) {
NSLog(@"ERROR: Could not remove SelfControl lock file.");
printStatus(-218);
exit(EX_IOERR);
}

[[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
Expand Down Expand Up @@ -516,7 +513,7 @@ void addRulesToFirewall(int controllingUID) {
// /etc/hosts blocking
if(!blockAsWhitelist) {
HostFileBlocker* hostFileBlocker = [[[HostFileBlocker alloc] init] autorelease];
if(![hostFileBlocker containsSelfControlBlock]) {
if(![hostFileBlocker containsSelfControlBlock] && [hostFileBlocker createBackupHostsFile]) {
[hostFileBlocker addSelfControlBlockHeader];
for(int i = 0; i < [domainList count]; i++) {
NSString* hostName;
Expand All @@ -539,6 +536,11 @@ void addRulesToFirewall(int controllingUID) {
}
[hostFileBlocker addSelfControlBlockFooter];
[hostFileBlocker writeNewFileContents];
} else if([hostFileBlocker containsSelfControlBlock]) {
[hostFileBlocker removeSelfControlBlock];
[hostFileBlocker writeNewFileContents];
} else {
NSLog(@"WARNING: Could not create backup file. Giving up on host file blocking.");
}
}

Expand Down Expand Up @@ -606,57 +608,72 @@ void addRulesToFirewall(int controllingUID) {

void removeRulesFromFirewall(int controllingUID) {
IPFirewall* firewall = [[IPFirewall alloc] init];
if([firewall containsSelfControlBlockSet]) {
HostFileBlocker* hostFileBlocker = [[[HostFileBlocker alloc] init] autorelease];
[hostFileBlocker removeSelfControlBlock];
[hostFileBlocker writeNewFileContents];
[firewall clearSelfControlBlockRuleSet];
NSLog(@"INFO: Blacklist blocking cleared.");
if(![firewall containsSelfControlBlockSet])
NSLog(@"WARNING: SelfControl rules do not appear to be loaded into ipfw.");
HostFileBlocker* hostFileBlocker = [[[HostFileBlocker alloc] init] autorelease];
[hostFileBlocker removeSelfControlBlock];
BOOL success = [hostFileBlocker writeNewFileContents];
// Revert the host file blocker's file contents to disk so we can check
// whether or not it still contains the block (aka we messed up).
[hostFileBlocker revertFileContentsToDisk];
[firewall clearSelfControlBlockRuleSet];
if(success && ![hostFileBlocker containsSelfControlBlock])
NSLog(@"INFO: Block successfully cleared.");
else {
NSLog(@"WARNING: Error removing host file block. Attempting to restore backup.");

// We'll play the sound now rather than putting it in the "defaults block"
// a few lines ago, because it is important that the UI get updated (by
// the posted notification) before we sleep to play the sound. Otherwise,
// the app seems unresponsive and slow.
[NSUserDefaults resetStandardUserDefaults];
seteuid(controllingUID);
defaults = [NSUserDefaults standardUserDefaults];
[defaults addSuiteNamed:@"org.eyebeam.SelfControl"];
if([defaults boolForKey: @"BlockSoundShouldPlay"]) {
// Map the tags used in interface builder to the sound
NSArray* systemSoundNames = [NSArray arrayWithObjects:
@"Basso",
@"Blow",
@"Bottle",
@"Frog",
@"Funk",
@"Glass",
@"Hero",
@"Morse",
@"Ping",
@"Pop",
@"Purr",
@"Sosumi",
@"Submarine",
@"Tink",
nil
];
NSSound* alertSound = [NSSound soundNamed: [systemSoundNames objectAtIndex: [defaults integerForKey: @"BlockSound"]]];
if(!alertSound)
NSLog(@"WARNING: Alert sound not found.");
else {
[alertSound play];
// Sleeping a second is a messy way of doing this, but otherwise the
// sound is killed along with this process when it is unloaded in just
// a few lines.
sleep(1);
}
if([hostFileBlocker restoreBackupHostsFile])
NSLog(@"INFO: Host file backup restored.");
else
NSLog(@"ERROR: Host file backup could not be restored. This may result in a permanent block.");
}

[hostFileBlocker deleteBackupHostsFile];

// We'll play the sound now rather than putting it in the "defaults block"
// a few lines ago, because it is important that the UI get updated (by
// the posted notification) before we sleep to play the sound. Otherwise,
// the app seems unresponsive and slow.
[NSUserDefaults resetStandardUserDefaults];
seteuid(controllingUID);
defaults = [NSUserDefaults standardUserDefaults];
[defaults addSuiteNamed:@"org.eyebeam.SelfControl"];
if([defaults boolForKey: @"BlockSoundShouldPlay"]) {
// Map the tags used in interface builder to the sound
NSArray* systemSoundNames = [NSArray arrayWithObjects:
@"Basso",
@"Blow",
@"Bottle",
@"Frog",
@"Funk",
@"Glass",
@"Hero",
@"Morse",
@"Ping",
@"Pop",
@"Purr",
@"Sosumi",
@"Submarine",
@"Tink",
nil
];
NSSound* alertSound = [NSSound soundNamed: [systemSoundNames objectAtIndex: [defaults integerForKey: @"BlockSound"]]];
if(!alertSound)
NSLog(@"WARNING: Alert sound not found.");
else {
[alertSound play];
// Sleeping a second is a messy way of doing this, but otherwise the
// sound is killed along with this process when it is unloaded in just
// a few lines.
sleep(1);
}
[defaults synchronize];
[NSUserDefaults resetStandardUserDefaults];
seteuid(0);
}
[defaults synchronize];
[NSUserDefaults resetStandardUserDefaults];
seteuid(0);

} else
NSLog(@"WARNING: SelfControl rules do not appear to be loaded into ipfw.");
// } else
// NSLog(@"WARNING: SelfControl rules do not appear to be loaded into ipfw.");
}

NSSet* getEvaluatedHostNamesFromCommonSubdomains(NSString* hostName, int port) {
Expand Down
9 changes: 9 additions & 0 deletions HostFileBlocker.h 100644 → 100755
Expand Up @@ -25,14 +25,23 @@

@interface HostFileBlocker : NSObject {
NSMutableString* newFileContents;
NSStringEncoding stringEnc;
}

- (BOOL)deleteBackupHostsFile;

- (BOOL)revertFileContentsToDisk;

- (BOOL)writeNewFileContents;

- (void)addSelfControlBlockHeader;

- (void)addSelfControlBlockFooter;

- (BOOL)createBackupHostsFile;

- (BOOL)restoreBackupHostsFile;

- (void)addRuleBlockingDomain:(NSString*)domainName;

- (BOOL)containsSelfControlBlock;
Expand Down
39 changes: 37 additions & 2 deletions HostFileBlocker.m 100644 → 100755
Expand Up @@ -30,16 +30,51 @@ @implementation HostFileBlocker

- (HostFileBlocker*)init {
if(self = [super init]) {
newFileContents = [NSMutableString stringWithContentsOfFile: kHostFileBlockerPath encoding: NSUTF8StringEncoding error: NULL];
newFileContents = [NSMutableString stringWithContentsOfFile: kHostFileBlockerPath usedEncoding: &stringEnc error: NULL];
if(!newFileContents)
return nil;
}

return self;
}

- (BOOL)revertFileContentsToDisk {
newFileContents = [NSMutableString stringWithContentsOfFile: kHostFileBlockerPath usedEncoding: &stringEnc error: NULL];
if(newFileContents) return YES;
else return NO;
}

- (BOOL)writeNewFileContents {
return [newFileContents writeToFile: kHostFileBlockerPath atomically: YES encoding: NSUTF8StringEncoding error: NULL];
return [newFileContents writeToFile: kHostFileBlockerPath atomically: YES encoding: stringEnc error: NULL];
}

- (BOOL)createBackupHostsFile {
NSFileManager* fileMan = [NSFileManager defaultManager];

if(![fileMan isReadableFileAtPath: @"/etc/hosts"] || [fileMan fileExistsAtPath: @"/etc/hosts.bak"])
return NO;

return [fileMan copyPath: @"/etc/hosts" toPath: @"/etc/hosts.bak" handler: nil];
}

- (BOOL)deleteBackupHostsFile {
NSFileManager* fileMan = [NSFileManager defaultManager];

if(![fileMan isDeletableFileAtPath: @"/etc/hosts.bak"])
return NO;

return [fileMan removeFileAtPath: @"/etc/hosts.bak" handler: nil];
}

- (BOOL)restoreBackupHostsFile {
NSFileManager* fileMan = [NSFileManager defaultManager];

if(![fileMan removeFileAtPath: @"/etc/hosts" handler: nil])
return NO;
if(![fileMan isReadableFileAtPath: @"/etc/hosts.bak"] || ![fileMan movePath: @"/etc/hosts.bak" toPath: @"/etc/hosts" handler: nil])
return NO;

return YES;
}

- (void)addSelfControlBlockHeader {
Expand Down
2 changes: 0 additions & 2 deletions Info.plist
Expand Up @@ -35,8 +35,6 @@
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.2.1</string>
<key>NSMainNibFile</key>
Expand Down
Empty file modified NSAddTemplate.jpg 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified NSCharacterSet+NewlineAddition.h 100644 → 100755
Empty file.
Empty file modified NSCharacterSet+NewlineAddition.m 100644 → 100755
Empty file.
Empty file modified NSRemoveTemplate.jpg 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 716f8fa

Please sign in to comment.