tags:

views:

496

answers:

1

Hi,

I'm running an uninstallation script through cocoa app, and I noticed that launchctl command doesn't get executed.

This is the part of the code that calls script (which contains sudo launchctl unload "name of the daemon")

//pFileName is the name of the File

NSString* pPath = [pCurrentBundle pathForResource:pFileName ofType:@"sh"];
char* const ppArgs[] = {const_cast<char*>([pPath fileSystemRepresentation]), NULL};

OSStatus status =  AuthorizationExecuteWithPrivileges(m_AuthorizationRef, "/bin/sh", kAuthorizationFlagDefaults, ppArgs, NULL);

Since all other script commands get executed properly (and it unloads fine in terminal), I am guessing that I don't have same privilege as root (both EUID and RUID being 0) as TN2083 states.

Has anyone encountered it before? Did you find any solutions?

A: 

First, you shouldn't be using sudo inside of a script that is already running as root. Under the default Mac setup it shouldn't cause a problem, but it depends on how the user has configured sudoers. This still would be my biggest suspicion of what's breaking.

Next, are you getting any error messages to console when this runs? You mentioned that other things are running; are they before or after this? Are you checking the unix error result from this script line?

I'll assume you've read Creating launchd Daemons and Agents.

You say that the script is running, so I'll assume that it actually has the extension .sh and that pFileName does not include the ".sh".

TN2083 makes no reference to AuthorizationExecuteWithPrivileges. What do you mean by "as TN2083 states?"

I assume you don't have setuid bits set on the script, correct? That will give you trouble with AuthorizationExecuteWithPrivileges.

Rob Napier
All you assumptions are correct. I had 'rm file' before and after launchctl, and when I noticed problem, the only command that wasn't properly executed was launchctl. Also when I mentioning TN2083, I was referring to "EUID and RUID being" 0 for launchctl to run.I don't have any setuid bits set on the script either. Uninstallation script has been working fine on other projects. But when I ported this to cocoa app, this is when I started seeing problem with launchctl.
Have you tried removing the 'sudo'? You're already root at this point.
Rob Napier
Yeah I tried both and still no luck.
Rob Napier
Not may—you should always use full paths, unless you hard-code a safe PATH in your application (which is another good idea). Otherwise, an attacker may install a malicious program somewhere and name it “launchctl”, and set the user's PATH via ~/.MacOSX/environment.plist so that your program runs it as root.
Peter Hosey