问题描述
好吧,我使用NSTask运行需要以管理员权限运行shell脚本。我使用的这个问题,一些code:。
以下是我目前有:
Ok, I am using NSTask to run a shell script that needs to be run with administrator privileges. I am using some code from this question: NSTask and AuthorizationCreate. Here is what I currently have:
//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)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"kandorBackup" ofType:@"sh"];
NSPipe *outputPipe = [NSPipe pipe];
readHandle = [outputPipe fileHandleForReading];
inData = nil;
returnValue = nil;
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:path];
[task setArguments:[NSArray arrayWithObjects:login, selectedDrive, rsyncFinalFlags, nil]];
[task setStandardOutput:outputPipe];
[readHandle waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(recievedData:)
name:NSFileHandleDataAvailableNotification
object:nil];
[task launch];
}
myStatus = AuthorizationFree (myAuthorizationRef,
kAuthorizationFlagDestroyRights);
在此执行,我提示您输入管理员用户名和密码,但随后的任务运行像通常那样,没有提升的权限。我在想什么做NSTask实际使用授权?
When this executes, I am prompted for an administrator username and password, but then the task runs like it normally would, without elevated privileges. What am I missing to make NSTask actually use the authorization?
推荐答案
在这里,你只是得到一个AuthorizationRef无所事事吧,刚开授权不会使你的可执行文件运行的特权。你可以用裁判来启动您使用SMJobSubmit您的应用程序和可执行文件中嵌入可执行文件,你可以使用NSTask提升的权限。下面是一些例子code解释是如何工作的:
Here you are just getting an AuthorizationRef and doing nothing with it, just getting the authorization doesn't make your executable run privileged. You can use the ref to launch an executable you embedded in your application using SMJobSubmit and within that executable, you can use NSTask with elevated permissions. Here is some example code explaining how this works: http://www.stairways.com/blog/2012-08-06-smjobsubmit
这篇关于使用授权服务与NSTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!