Given that there is a file called copystuff in the Resources folder in a an xCode project, and that file reads:
#!/bin/sh
cp -R /Users/someuser/Documents /Users/admin/Desktop
And if this bit of code below is linked to a button in IB … it will copy the /Users/someuser/Documents directory to /Users/admin when the button is pressed in a Cocoa app… It works when app is launched in an admin account ( using OS X 10.5.x here) …
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle]
pathForResource:@"copystuff" ofType:@"sh"], nil]];
[task launch];
My question is.. is there a way to have NSTask run a script running as root while this code is called from a non-admin account? Or asked another way..can Objective-C be coded to run scripts from say /usr/bin as root from a non-admin account?
I would strongly recommend against using an external script like this. It’s much better to do this with
NSFileManagerand keep this inside of controlled code. But to the question of how to become root, you want to look at Authorization Services. This site will walk you through how to create an application that escalates its privileges, including the appropriate UI elements for it.