Executing shell script with root privilege
Executing shell script with root privilege
- Subject: Executing shell script with root privilege
- From: cocoa learner <email@hidden>
- Date: Sun, 14 Feb 2010 11:35:07 +0530
Hi all,
I want to execute a shell script with "root" privilege and here is the code
for that I am using -
- (BOOL) launchScript
{
OSStatus authStatus;
AuthorizationRef authRef = nil;
authStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authRef);
if (authStatus != errAuthorizationSuccess)
{
return FALSE;
}
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = (kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights);
authStatus = AuthorizationCopyRights(authRef, &rights, NULL, flags, NULL);
if (authStatus != errAuthorizationSuccess)
{
AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
return FALSE;
}
char *cmd = "/Users/test/myScript";
char *args[] = {NULL};
FILE *pipe = NULL;
authStatus = AuthorizationExecuteWithPrivileges(authRef,
cmd,
kAuthorizationFlagDefaults,
args,
&pipe);
if (authStatus == errAuthorizationSuccess)
{
return TRUE;
}
return FALSE;
}
And the shell script looks like this -
#!/bin/bash
WHO=`whoami`
echo "I am executed as [ $WHO ]" > /tmp/debugScript
id -u >> /tmp/debugScript
And permission of this script is -
-rwx------ 1 root wheel 536 Feb 14 10:51 /Users/test/myScript
When I am executing the app "launchScript" is returning me TRUE but my
script is not getting executed. I am totally lost, have no clue why it's
happening like this.
Does any body know why this script is not getting executed? Am I doing any
thing wrong?
Regards
Cocoa.learner
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden