获取管理员权限从一个可可应用程序删除使用RM文件 [英] Obtaining admin privileges to delete files using rm from a Cocoa app

查看:238
本文介绍了获取管理员权限从一个可可应用程序删除使用RM文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个小应用程序,删除日志文件。我使用它运行RM和SRM(安全RM)的NSTask实例来删除文件。

I am making a small app that deletes log files. I am using an NSTask instance which runs rm and srm (secure rm) to delete files.

我希望能够在删除文件:

I want to be able to delete files in:


  • /库/日志

  • 〜/资源库/日志

问题是用户帐户没有权限来访问某些文件系统中的库文件夹,如登录的Adobe子文件夹等。例如,只有系统用户(组?)具有读/写权限的Adobe日志文件夹及其内容,与当前用户甚至没有在获取信息窗口中显示文件夹的权限的项

The issue is that the user account does not have permissions to access some files in the system library folder, such as the Adobe logs subfolder and others. For example, only the "system" user (group?) has r/w permissions for the Adobe logs folder and its contents, and the current user doesn't even have an entry in the permissions shown in the Get Info window for the folder.

我希望能够做的事情:


  1. 获取管理员权限。

  2. 存储在Keychain的密码,以便应用程序不必每次都唠叨用户(密码是一个坏主意的存储?这可能吗?)

  3. 删除任何文件权限可能是一个文件。

我使用NSTask,因为它提供通知任务的完成,正从任务本身的文本输出等。我需要使用别的东西吗?如果是这样,我怎么能复制NSTask的完成通知和输出文件句柄在运行RM和SRM具有管理员权限?

I am using NSTask because it offers notifications for task completion, getting text output from the task itself, etc. Would I need to use something else? If so, how could I replicate NSTask's completion notifications and output file handle while running rm and srm with admin privileges?

我要找来处理这种情况的最安全的方式。即我不希望我的应用程序成为特权升级攻击门口。

I am looking for the most secure way to handle the situation. i.e. I don't want my application to become a doorway for privilege escalation attacks.

我看着授权服务编程指南,但我不知道这种情况相符。起初我以为 AuthorizationExecuteWithPrivileges 将是一个不错的主意,但阅读更多关于它看起来像这样方法的主体,不建议出于安全原因后。

I looked at the Authorization Services Programming Guide but I am not sure which case fits. At first I thought that AuthorizationExecuteWithPrivileges would be a good idea but after reading more on the subject it looks like this method is not recommended for security reasons.

一个详细的答复将是非常欢迎的。我敢肯定,你们中的一些已经有做类似的东西,有一些code和知识共享。

A detailed answer would be very welcome. I'm sure some of you already had to do something similar and have some code and knowledge to share.

在此先感谢!

更新:

我现在能够使身份验证对话框弹出,并获得特权,就像这样:

I am now able to make the authentication dialog pop up and obtain privileges, like so:

OSStatus status;
AuthorizationRef authRef;
    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);

AuthorizationRights authRights;
AuthorizationItem authItems[1];

authItems[0].name = kAuthorizationRightExecute;

authRights.count = sizeof(authItems) / sizeof(authItems[0]);
authRights.items = authItems;

AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed;

status = AuthorizationCopyRights(authRef, &authRights, kAuthorizationEmptyEnvironment, authFlags, NULL);

从外观上来看,似乎保理应用程序的方法看起来最合适的。事情是这样的,对我来说,的 RM似乎已经像一个外部辅助工具的。我不知道我得到的文件中提出了setuid选择。我可以设置对RM setuid位,并使用NSTask方法我已经实现了运行呢?这意味着我不需要创建我自己的助手工具。可能有人对这个问题解释一下?

From the looks of it, it seems that the "Factored Application" method looks the most appropriate. The thing is that, to me, rm already seems like an external helper tool. I'm not sure I get the setuid alternative suggested in the documentation. Could I set the setuid bit on rm and run it using the NSTask method I already implemented? This would mean that I wouldn't need to create my own helper tool. Could somebody elaborate on this subject?

我也看了其中建议为setuid位的方法更安全,最近的另类BetterAuthorizationSample,但发现它非常复杂,如简单的行为。任何提示?

I also looked at the BetterAuthorizationSample which is suggested as a more secure and recent alternative to the setuid bit method, but found it awfully complex for such as simple behavior. Any hints?

在此先感谢您的帮助!

推荐答案

我在几个月前就有这种头痛。我试图让一个shell脚本,在一定的时间与管理员权限运行关机我的电脑。我觉得你的痛苦。

I had this headache a few months ago. I was trying to get a shell script running with admin privileges that shutdown my computer at a certain time. I feel your pain.

我使用的是一个总的噩梦涉水通过BetterAuthorizationSample。但我选择了最务实的路线 - 我没有理会试图理解这是怎么回事,我只是抓住了code的胆量一切

I used the BetterAuthorizationSample which was a total nightmare to wade through. But I took the most pragmatic route - I didn't bother trying to understand everything that was going on, I just grabbed the guts of the code.

它没有把我相当长的时间做它什么我想要的。我不记得确切我改变什么,但欢迎您看看我的code:

It didn't take me that long to get it doing what I wanted. I can't remember exactly what I altered, but you're welcome to check out my code:

http://github.com/johngallagher/TurnItOff

我希望这有助于你的追求一个安全的应用程序!

I hope this helps on your quest for a secure application!

这篇关于获取管理员权限从一个可可应用程序删除使用RM文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆