Cocoa - 获得 NSFileManager 的根访问权限

发布于 2024-10-10 01:51:25 字数 160 浏览 6 评论 0原文

我需要在应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-10-17 01:51:25

更新:为了更新仍在使用此答案作为参考的人,BLAuthentication 使用了一个旧的且高度不推荐的函数,称为 AuthorizationExecuteWithPrivileges,在工作时,违背了现代安全范式,并且已被弃用(并且已经有一段时间了)。从技术上讲,您仍然可以使用它,但如果您正在为 Mac OS X Lion 进行开发,我们非常欢迎您使用 ServicesManagement 框架,该框架允许您以辅助工具的权限运行代码。

有关如何创建和启动特权帮助程序工具的参考,请查看我的问题之一,使用 SMJobBless() 编写特权辅助工具


没有真正简单的方法来授权 NSFileManager,因此您应该考虑使用在管理员身份验证下运行的标准 mvcp 工具以及 >BLAuthentication 类。不幸的是,原作者的网站已关闭,但您可以轻松地在 Google 上找到该课程的副本(如果您愿意,我也可以为您上传副本)。


使用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];

上面的代码将提示用户输入管理员密码,并在五分钟的默认时间限制内对程序进行身份验证。


警告
当然,一定要小心系统文件!尽可能避免移动或操纵它们,特别是如果您的程序将在其他人的计算机上运行(如果出现任何问题,您将受到指责)!

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). 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 standard mv and cp tools run under administrator authentication with the BLAuthentication 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:

#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.


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)!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文