2012-12-06 3 views
1

내 코코아 응용 프로그램에서 루트 권한으로 다른 응용 프로그램을 실행해야하지만 작동하지 않습니다! NSTask에는 필요한 권한이 없습니다. AuthorizationCreate 섹션에서 필요한 권한을 얻을 수 있습니다. NSTask를 실행하면 다른 응용 프로그램을 실행하는 부분입니다.NSTask 및 AuthorizationCreate

myStatus는 errAuthorizationSuccess를 반환합니다.

여기에 코드

//Authorization Create 
    AuthorizationRef myAuthorizationRef; 
OSStatus myStatus; 
myStatus = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment, 
           kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed , &myAuthorizationRef); 
AuthorizationItem myItems[1]; 

myItems[0].name = "com.myOrganization.myProduct.myRight1"; 
myItems[0].valueLength = 0; 
myItems[0].value = NULL; 
myItems[0].flags = 0; 

AuthorizationRights myRights; 
myRights.count = sizeof (myItems)/sizeof (myItems[0]); 
myRights.items = myItems; 

AuthorizationFlags myFlags; 
myFlags = kAuthorizationFlagDefaults | 
kAuthorizationFlagInteractionAllowed | 
kAuthorizationFlagExtendRights; 

myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights, 
            kAuthorizationEmptyEnvironment, myFlags, NULL); 

if (myStatus==errAuthorizationSuccess) 
{ 

//Running NSTask 
    //setting license 
    NSTask *task; 
    task = [[NSTask alloc] init]; 
    [task setLaunchPath: @"/opt/cprocsp/sbin/cpconfig"]; 

    NSArray *arguments; 
    arguments = [NSArray arrayWithObjects:@"-license", @"-set", 
      @"lalala", nil]; 
    [task setArguments: arguments]; 

    NSPipe *pipe; 
    pipe = [NSPipe pipe]; 
    [task setStandardOutput: pipe]; 

    NSFileHandle *file; 
    file = [pipe fileHandleForReading]; 

    [task launch]; 

    NSData *data; 
    data = [file readDataToEndOfFile]; 

    NSString *string; 
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 
    NSLog (@"grep returned:\n%@", string); 
    [_logs insertText:string]; 
} 
myStatus = AuthorizationFree (myAuthorizationRef, 
           kAuthorizationFlagDestroyRights); 

내가 STPrivilege 그 때문에 ARC 프로젝트를 사용할 수 없습니다에게 있습니다. 내가 뭘 잘못하고있어? Thx 도움.

+0

이에 대한 답변을합니까. 질문 도움? http://stackoverflow.com/questions/4050687/how-to-use-nstask-as-root – trojanfoe

+0

Nope :( AuthorizeExecuteWithPrivileges()는 더 이상 사용되지 않으며 STPrivilegedTask는 내가 쓸 수 없으므로 ( – Dimson

+0

) 앱이 샌드 박스로되어 있습니까? 인증 서비스가있는 경우 작동하지 않습니다. – sjs

답변

0

내가 잘못하고 있었는지 알고 있지만, 아주 이상한 솔루션으로 그것을 해결하지 않습니다 : (((

NSDictionary *error = [NSDictionary dictionary]; 
NSAppleScript *run = [[NSAppleScript alloc]initWithSource:@"do shell script \"/opt/cprocsp/sbin/cpconfig -license -set lala\" with administrator privileges"]; 
[run executeAndReturnError:&error]; 

내가 sooo를 많이 좋아하지 않았다

+0

나는 당신과 같은 문제가 있었지만 에스컬레이트 된 권한을 사용하여 쉘에서 실행하는 다른 명령을 사용했습니다. 그래서 NSTask 대신 NSAppleScript를 사용하여 작동하게 만들었습니까? – Aryo

+0

예, 효과가있었습니다. – Dimson