Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Created June 19, 2013 05:29
Show Gist options
  • Save TomLiu/5811875 to your computer and use it in GitHub Desktop.
Save TomLiu/5811875 to your computer and use it in GitHub Desktop.
Ask for privilege to execute command
//
// NSAuthorization.h
// OSXvnc
//
// Created by Jonathan Gillaspie on Fri Dec 12 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <Security/Authorization.h>
#include <Security/AuthorizationTags.h>
@interface NSAuthorization : NSObject {
// Persistent AuthRef
AuthorizationRef myAuthorizationRef;
}
- init;
- (BOOL) executeCommand:(NSString *) command withArgs: (NSArray *) argumentArray;
- (BOOL) executeCommand:(NSString *) command withArgs: (NSArray *) argumentArray synchronous: (BOOL) sync;
@end
//
// NSAuthorization.m
// OSXvnc
//
// Created by Jonathan Gillaspie on Fri Dec 12 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
#import "NSAuthorization.h"
#include "unistd.h"
@implementation NSAuthorization
- init {
AuthorizationFlags myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
OSStatus myStatus;
AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights myRights = {1, &myItems};
[super init];
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
// This will pre-authorize the authentication
if (myStatus == errAuthorizationSuccess)
myStatus = AuthorizationCopyRights(myAuthorizationRef, &myRights, NULL, myFlags, NULL);
if (myStatus != errAuthorizationSuccess) {
return nil;
}
return self;
}
- (BOOL) executeCommand:(NSString *) command withArgs: (NSArray *) argumentArray {
return [self executeCommand:(NSString *) command withArgs: (NSArray *) argumentArray synchronous:TRUE];
}
- (BOOL) executeCommand:(NSString *) command withArgs: (NSArray *) argumentArray synchronous: (BOOL) sync {
FILE *communicationStream = NULL;
char **copyArguments = NULL;
int i;
OSStatus myStatus;
char outputString[1024];
int startTime=time(NULL);
copyArguments = malloc(sizeof(char *) * ([argumentArray count]+1));
for (i=0;i<[argumentArray count];i++) {
copyArguments[i] = (char *) [[argumentArray objectAtIndex:i] lossyCString];
}
copyArguments[i] = NULL;
myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef,
[command UTF8String],
kAuthorizationFlagDefaults,
copyArguments,
(sync ? &communicationStream : NULL)); // FILE HANDLE for I/O
if (myStatus==errAuthorizationSuccess && sync) {
while (!myStatus && !feof(communicationStream) && fgets(outputString, 1024, communicationStream) && time(NULL)-startTime<10) {
if (strlen(outputString) > 1)
NSLog(@"NSAuthorization: %s",outputString);
}
fclose(communicationStream);
}
free(copyArguments);
if (myStatus != errAuthorizationSuccess)
NSLog(@"Error: Executing %@ with Authorization: %ld", command, myStatus);
return (myStatus == errAuthorizationSuccess);
}
- (void) dealloc {
AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults);
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment