Skip to content

Commit

Permalink
fix prompt for 10.7
Browse files Browse the repository at this point in the history
  • Loading branch information
kalikaneko committed Jun 19, 2013
1 parent bd2e15a commit bf133b4
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions cocoasudo.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ int cocoaSudo(char *executable, char *commandArgs[], char *icon, char *prompt) {
OSStatus status;
AuthorizationRef authRef;

AuthorizationItem right = { "com.performant.cocoasudo", 0, NULL, 0 };
AuthorizationRights rightSet = { 1, &right };
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rightSet = {1, &right};

AuthorizationEnvironment myAuthorizationEnvironment;
AuthorizationItem kAuthEnv[2];
myAuthorizationEnvironment.items = kAuthEnv;

AuthorizationFlags flags = kAuthorizationFlagDefaults;

myAuthorizationEnvironment.items = kAuthEnv;

if (prompt && icon) {
kAuthEnv[0].name = kAuthorizationEnvironmentPrompt;
Expand Down Expand Up @@ -136,32 +138,39 @@ int cocoaSudo(char *executable, char *commandArgs[], char *icon, char *prompt) {
myAuthorizationEnvironment.count = 0;
}

if (AuthorizationCreate(NULL, &myAuthorizationEnvironment, kAuthorizationFlagDefaults, &authRef) != errAuthorizationSuccess) {
status = AuthorizationCreate(NULL, &myAuthorizationEnvironment, flags, &authRef);

if (status != errAuthorizationSuccess) {
NSLog(@"Could not create authorization reference object.");

status = errAuthorizationBadAddress;
}
else {
status = AuthorizationCopyRights(authRef, &rightSet, &myAuthorizationEnvironment, kAuthorizationFlagDefaults | kAuthorizationFlagPreAuthorize | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights, NULL);
flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;

status = AuthorizationCopyRights(authRef, &rightSet, &myAuthorizationEnvironment, flags, NULL);
}

if (status == errAuthorizationSuccess) {
FILE *ioPipe;
char buffer[1024];
int bytesRead;

status = AuthorizationExecuteWithPrivileges(authRef, executable, 0, commandArgs, &ioPipe);
flags = kAuthorizationFlagDefaults;
status = AuthorizationExecuteWithPrivileges(authRef, executable, flags, commandArgs, &ioPipe);

/* Just pipe processes' stdout to our stdout for now; hopefully can add stdin pipe later as well */

for (;;) {
for (;;) {
bytesRead = fread(buffer, sizeof(char), 1024, ioPipe);

if (bytesRead < 1) {
break;
}
if (bytesRead < 1) {
break;
}

write(STDOUT_FILENO, buffer, bytesRead * sizeof(char));
write(STDOUT_FILENO, buffer, bytesRead * sizeof(char));
}

pid_t pid;
Expand All @@ -170,20 +179,19 @@ int cocoaSudo(char *executable, char *commandArgs[], char *icon, char *prompt) {
do {
pid = wait(&pidStatus);
}
while (pid != -1);
while (pid != -1);

if (status == errAuthorizationSuccess) {
if (status == errAuthorizationSuccess) {
retVal = 0;
}
}
else {
AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
authRef = NULL;

if (status != errAuthorizationCanceled) {
if (status != errAuthorizationCanceled) {
// pre-auth failed

NSLog(@"Pre-auth failed");
NSLog(@"Pre-auth failed");
}
}

Expand Down

0 comments on commit bf133b4

Please sign in to comment.