Cocoa - 获得 NSFileManager 的根访问权限
我需要在应用程序中使用 NSFileManager 移动系统文件,但我似乎没有 root 访问权限。获得此特权的最简单方法是什么?
我研究了 Apple 提供的 BetterAuthorizationSample 代码,我似乎无法让 NSFileManager 在获得用户批准后运行其任务。
I need to move system files with NSFileManager in my application and I don't seem to have root access. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:为了更新仍在使用此答案作为参考的人,
BLAuthentication
使用了一个旧的且高度不推荐的函数,称为AuthorizationExecuteWithPrivileges
,在工作时,违背了现代安全范式,并且已被弃用(并且已经有一段时间了)。从技术上讲,您仍然可以使用它,但如果您正在为 Mac OS X Lion 进行开发,我们非常欢迎您使用 ServicesManagement 框架,该框架允许您以辅助工具的权限运行代码。有关如何创建和启动特权帮助程序工具的参考,请查看我的问题之一,使用 SMJobBless() 编写特权辅助工具。
没有真正简单的方法来授权
NSFileManager
,因此您应该考虑使用在管理员身份验证下运行的标准mv
和cp
工具以及>BLAuthentication
类。不幸的是,原作者的网站已关闭,但您可以轻松地在 Google 上找到该课程的副本(如果您愿意,我也可以为您上传副本)。使用
BLAuthentication
,您尝试执行的操作如下所示:上面的代码将提示用户输入管理员密码,并在五分钟的默认时间限制内对程序进行身份验证。
警告
当然,一定要小心系统文件!尽可能避免移动或操纵它们,特别是如果您的程序将在其他人的计算机上运行(如果出现任何问题,您将受到指责)!
Update: To update people still using this answer for reference,
BLAuthentication
makes use of an old, and highly unrecommended function calledAuthorizationExecuteWithPriviledges
that, while working, goes against the modern security paradigm, and is deprecated (and has been for a while). 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.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().
There's no real easy way to authorize
NSFileManager
, so you should look into using the standardmv
andcp
tools run under administrator authentication with theBLAuthentication
class. 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).With
BLAuthentication
, what you are trying to do goes something like this:The code above will prompt the user for the administrator's password and authenticate the program for the default time limit of five minutes.
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)!
如果您的应用程序需要使用 root 权限,请使用 Apple 的授权服务 API。
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html#//apple_ref/doc/uid/TP30000995-CH204-TP1
If you application needs to use root privileges, use Apple's Authorization Services API.
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html#//apple_ref/doc/uid/TP30000995-CH204-TP1