Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created April 24, 2010 21:24
Show Gist options
  • Save mrchrisadams/377960 to your computer and use it in GitHub Desktop.
Save mrchrisadams/377960 to your computer and use it in GitHub Desktop.
//
// AuthHelperTool.m
// BDAuthorize
//
// Created by Brian Dunagan on 11/23/08.
// Copyright 2008 bdunagan.com. All rights reserved.
//
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"AuthHelperTool started");
// Look at arguments.
if (argc == 3)
{
// Hasn't been called as root yet.
NSLog(@"AuthHelperTool executing self-repair");
// Paraphrased from http://developer.apple.com/documentation/Security/Conceptual/authorization_concepts/03authtasks/chapter_3_section_4.html
OSStatus myStatus;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
if (myStatus != errAuthorizationSuccess)
return myStatus;
AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights myRights = {1, &myItems};
myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights, NULL, myFlags, NULL );
if (myStatus != errAuthorizationSuccess)
return myStatus;
char *myToolPath = argv[1];
char *myArguments[] = {argv[1], "--self-repair", argv[2], NULL};
FILE *myCommunicationsPipe = NULL;
myFlags = kAuthorizationFlagDefaults;
myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, myToolPath, myFlags, myArguments, &myCommunicationsPipe);
NSLog(@"AuthHelperTool called AEWP");
}
else
{
if (argc == 4)
{
NSString *command = [NSString stringWithCString:argv[3]];
NSLog(@"AuthHelperTool sent command %@", command);
if ([command isEqualToString:@"create"])
{
NSLog(@"AuthHelperTool executing %@", command);
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:@"world" forKey:@"hello"];
BOOL isSuccess = [dictionary writeToFile:@"/Library/LaunchDaemons/com.bdunagan.bdauthorize.plist" atomically:NO];
NSLog(@"AuthHelperTool done with %@: %d", command, isSuccess);
}
if ([command isEqualToString:@"duplicate"])
{
NSLog(@"AuthHelperTool executing %@", command);
// setuid is necessary to execute sudo.
setuid(0);
NSArray *args = [NSArray arrayWithObjects: @"cp", @"/Library/LaunchDaemons/com.bdunagan.bdauthorize.plist", @"/Library/LaunchDaemons/com.bdunagan.bdauthorize.plist.duplicate", nil];
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/sudo" arguments:args];
NSLog(@"AuthHelperTool done with %@: ?", command);
}
}
}
NSLog(@"AuthHelperTool exiting");
[pool release];
return 0;
}
//
// BDAuthorize.h
// BDAuthorize
//
// Created by Brian Dunagan on 11/23/08.
// Copyright 2008 bdunagan.com. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface BDAuthorize : NSObject
{
NSString *helperToolPath;
}
- (IBAction)clickCreateFile:(id)sender;
- (IBAction)clickDuplicateFile:(id)sender;
@end
//
// BDAuthorize.m
// BDAuthorize
//
// Created by Brian Dunagan on 11/23/08.
// Copyright 2008 bdunagan.com. All rights reserved.
//
#import "BDAuthorize.h"
@implementation BDAuthorize
- (void)awakeFromNib
{
helperToolPath = [[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/AuthHelperTool"] retain];
}
- (IBAction)clickCreateFile:(id)sender
{
NSArray *args = [NSArray arrayWithObjects:helperToolPath, @"create", nil];
[NSTask launchedTaskWithLaunchPath:helperToolPath arguments:args];
}
- (IBAction)clickDuplicateFile:(id)sender
{
NSArray *args = [NSArray arrayWithObjects:helperToolPath, @"duplicate", nil];
[NSTask launchedTaskWithLaunchPath:helperToolPath arguments:args];
}
@end
class BDAuthorize
# this is an attempt at replacing the two BDAuthorize.m, and BDAuthorize.h files.
# As far as I can tell, I should be able to treat AuthHelperTool like a black box - I tried to start converting it, but got hopelessly lost in the process.
# I assuming helpertoolpath should be an instance variable on the ruby class
@helpertoolpath = File.join(NSBundle.mainBundle.resourcePath, 'AuthHelperTool')
# macruby should automatically make these methods available to interface builder
# however, the method 'launchedTaskWithLaunchPath' doesn't seem to be available when I create an NSTask instance in macirb
def clickCreateFile
NSTask.alloc.launchedTaskWithLaunchPath(@helpertoolpath, arguments:["create", nil])
end
def clickDuplicateFile
NSTask.launchedTaskWithLaunchPath(@helpertoolpath, arguments:["duplicate", nil])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment