2

I'm writing an app that needs to overwrite a read-only file. Specifically the /etc/hosts file. To do that I need to ask for permission from the user to get root access and thereby permission to overwrite the hosts file.

I have searched all over the internet, but I have not been able to understand how to do this. I realize that a method called AuthorizationExecuteWithPrivileges() used to be used for this, but that it is deprecated in OSX 10.7. Then I came across something called SMJobBless, but I really didn't understand the concept of that?

So can you explain how to solve my problem the right way?

Regards, Rasmus

Michael Dautermann
  • 86,557
  • 17
  • 155
  • 196
Tokke
  • 272
  • 1
  • 12
  • 1
    `chmod 7777 AppExecutable` and call `setuid(0);` in `main()` and you'll be root, tadam! (standard way for getting root in jailbroken iOS apps, btw.) –  Nov 21 '12 at 22:49
  • Thanks a lot for the reply! I'm kind of new to obj-c, so I wondered if you maybe could elaborate on specifically how to do this? :) – Tokke Nov 21 '12 at 23:25
  • the first one is to be typed into the command line. I don't know what I could elaborate on the second part, I told you to call `setuid(0);` from `main()`, that's it... –  Nov 22 '12 at 05:30
  • @H2CO3 Is it possible to run that command at the beginning of the program from inside the program so the user doesn't have to type that command himself? – Tokke Nov 25 '12 at 09:11
  • the user doesn't have to do anything. You chmod the binary and distribute it as such. –  Nov 25 '12 at 09:19
  • Just to be clear. I run the command on my own machine and then when i distribute that .app file the chmod setting is maintained? – Tokke Nov 25 '12 at 10:01
  • if you use an appropriate package manager (APT for example), then it will. –  Nov 25 '12 at 12:31

1 Answers1

3

http://atnan.com/blog/2012/02/29/modern-privileged-helper-tools-using-smjobbless-plus-xpc/

SMJobBless + XPC is a os x 10.7+ solution for what you require.

The provided link contains source code showing examples too.

Oliver Atkinson
  • 7,647
  • 29
  • 41