简体   繁体   English

Cocoa - 获取NSFileManager的Root访问权限

[英]Cocoa - Gaining Root Access for NSFileManager

I need to move system files with NSFileManager in my application and I don't seem to have root access. 我需要在我的应用程序中使用NSFileManager移动系统文件,我似乎没有root访问权限。 What would be the easiest way to go about gaining this privilege? 获得此特权的最简单方法是什么?

I have looked into the BetterAuthorizationSample code provided by Apple, and I don't seem how I could have the NSFileManager run its task once its been given approval by the user. 我已经查看了Apple提供的BetterAuthorizationSample代码,我似乎并没有让NSFileManager在获得用户批准后运行其任务。

Update: To update people still using this answer for reference, BLAuthentication makes use of an old, and highly unrecommended function called AuthorizationExecuteWithPriviledges that, while working, goes against the modern security paradigm, and is deprecated (and has been for a while). 更新:为了更新仍在使用此答案供参考的人, BLAuthentication使用了一个名为AuthorizationExecuteWithPriviledges的旧的,非常推荐的函数,该函数在工作时违反现代安全范例,并且已弃用(并且已经有一段时间了)。 You're still allowed to use it, technically, but if you're developing for Mac OS X Lion, you're more than welcome to use the ServicesManagement framework, that allows you to run code with privileges as a helper tool. 从技术上讲,您仍然可以使用它,但如果您正在为Mac OS X Lion开发,那么您非常欢迎使用ServicesManagement框架,它允许您以特权作为帮助工具运行代码。

For reference on how to create and launch a privileged helper tool, take a look at one of my questions, Writing a Privileged Helper Tool with SMJobBless() . 有关如何创建和启动特权帮助工具的参考,请查看我的一个问题, 使用SMJobBless编写特权帮助工具()


There's no real easy way to authorize NSFileManager , so you should look into using the standard mv and cp tools run under administrator authentication with the BLAuthentication class. 没有真正简单的方法来授权NSFileManager ,因此您应该考虑使用在BLAuthentication类的管理员身份验证下运行的标准mvcp工具。 Unfortunately, the original author's website is down, but you can easily find copies of the class floating around on Google (I can also upload a copy for you if you wish). 不幸的是,原作者的网站已关闭,但您可以轻松找到在Google上浮动的课程副本(如果您愿意,我也可以为您上传副本)。


With BLAuthentication , what you are trying to do goes something like this: 使用BLAuthentication ,您要做的事情是这样的:

#define MOVE @"/bin/mv"
if (![[BLAuthentication sharedInstance] isAuthenticated:MOVE]) {
    [[BLAuthentication sharedInstance] authenticate:MOVE];
}

NSArray *arguments = [NSArray arrayWithObjects:@"location1", @"location2", nil];
[[BLAuthentication sharedInstance] executeCommand:MOVE withArgs:arguments];

The code above will prompt the user for the administrator's password and authenticate the program for the default time limit of five minutes. 上面的代码将提示用户输入管理员密码,并在5分钟的默认时间限制内对程序进行身份验证。


WARNING 警告
Of course, always be careful with system files! 当然,要小心系统文件! Avoid moving or manipulating them when possible, especially if your program is going to be run on someone else's computer (if anything goes wrong, you're going to be blamed)! 尽可能避免移动或操纵它们,特别是如果你的程序将在别人的计算机上运行(如果出现任何问题,你将被指责)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM