• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: nstask with admin privs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: nstask with admin privs


  • Subject: Re: nstask with admin privs
  • From: Andrew Satori <email@hidden>
  • Date: Fri, 12 Jan 2007 16:37:58 -0500

When I did this a year or so ago, it did not come up with a uid of 0, it was always the uid of the admin user (in this case my admin account 501, instead of my user account 502). There was an euid of 0, but the uid was not 0 until I set it.

Perhaps something has changed, perhaps it was a bug in my code. I battled it for a couple of days, asked on the lists, got nothing so I implemented the creative engineering solution you see below, and have not revisited this since then.

I suppose I could go test this, but I'm at work where I'm a Windows dev, so I can't right this minute.

The quick test: write a bash script that calls 'ConsoleMessage "Logging Foo to the Console.log"'. Run it in Terminal, you'll get "you must be root to run ConsoleMessage". Next, run it with Admin Priv's from the example (see code):

Modify said script to ALSO print the UID's. and see what you get. Follow that with the helper. Take it all with a grain of salt, after all, I know a helluva lot more about Windows than the mix and match Mac/Unix that is part an parcel to dealing with Security on the Mac.

Andy

- (void)execWithRights
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

OSStatus myStatus;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;

NSBundle *bundleApp = [NSBundle mainBundle];
NSString *pathToHelper = [bundleApp pathForResource:@"StartupHelper" ofType:nil];

// myAuthorizationItem.AuthorizationString = "@
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
myFlags, &myAuthorizationRef);
if (myStatus != errAuthorizationSuccess)
return;


do
{
AuthorizationItem myItems = {kAuthorizationRightExecute, [pathToHelper length], [pathToHelper cString], 0};
AuthorizationRights myRights = {1, &myItems};


		myFlags =  kAuthorizationFlagDefaults |
				kAuthorizationFlagInteractionAllowed |
				kAuthorizationFlagPreAuthorize |
				kAuthorizationFlagExtendRights;
		myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights,
			kAuthorizationEmptyEnvironment, myFlags, NULL );

        if (myStatus == errAuthorizationSuccess)
		{
			const char *myToolPath = [pathToHelper cString];
			char *myArguments[3];

			myArguments[0] = [command cString];
			myArguments[1] = [operation cString];
			myArguments[2] = NULL;

			FILE *myCommunicationsPipe = NULL;
			char myReadBuffer[128];

			myFlags = kAuthorizationFlagDefaults;
			myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef,
					myToolPath, myFlags, myArguments, &myCommunicationsPipe);

			if (myStatus == errAuthorizationSuccess)
			for(;;)
			{
				int bytesRead = read (fileno (myCommunicationsPipe),
						myReadBuffer, sizeof (myReadBuffer));
				if (bytesRead < 1) break;
				NSLog(@"%s", myReadBuffer);
			}
		}
    } while (0);

    AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults);

    if (myStatus) NSLog(@"Status: %i\n", myStatus);

    [working stopAnimation:nil];

    [pool release];
    [NSThread exit];

    return;
}



On Jan 12, 2007, at 3:43 PM, Michael Watson wrote:

Something about your approach seems quite off. I can tell you that I've launched helper tools with privilege via AuthorizationExecuteWithPrivileges() and logged their UID as 0, without additional hackery. Is this not what you're after?


-- m-s


On 12 Jan, 2007, at 15:37, Andrew Satori wrote:

There is a wrinkle here though.

When you call shell task using the articles below, you do have admin rights, but not "root". This may or may not be a problem, for me it was, since I was calling something that checked for Root and would exit if it was not root, so even though I had sufficient rights, I didn't have 'root'. You can hack around this though.

What I did was assembled a little helper tool, that I run with Admin privileges, that takes the command I want to execute on an argument on the command line, and then escalates itself to correct privs ala:


// main.c

#include <stdio.h>

#include <sys/types.h>
#include <unistd.h>

int main (int argc, const char * argv[]) {
// insert code here...
// printf("Calling the StartupItem %s with parameter %s.\n", argv[1], argv[2]);
setuid(0);

char* szCommand = (char *)malloc(strlen(argv[1]) + strlen(argv[2] + 2));

int x = 0;
for (x = 1; x < argc; x++)
{
sprintf(szCommand, "%s %s", szCommand, argv[x]);
}

// printf("%s\n", szCommand);
system(szCommand);

free(szCommand);

return 0;
}


and called that using the below articles.

Andy




On Jan 12, 2007, at 12:19 PM, Michael Watson wrote:

You won't use NSTask, but this can be done:

http://developer.apple.com/documentation/Security/Conceptual/ Security_Overview/index.html

http://developer.apple.com/documentation/Security/Conceptual/ authorization_concepts/index.html


-- m-s

On 12 Jan, 2007, at 11:51, Ian Archer wrote:

Is it possible to run a subtask through something like NSTask with
admin/sudo priviledges?

Ideally I'm looking for something which prompts the user for a
password before launching a secure subprocess. I don't want the main
application to have to be run by admin, though.
_______________________________________________


Cocoa-dev mailing list (email@hidden)

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:
40bungie.org


This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


References: 
 >nstask with admin privs (From: "Ian Archer" <email@hidden>)
 >Re: nstask with admin privs (From: Michael Watson <email@hidden>)
 >Re: nstask with admin privs (From: Andrew Satori <email@hidden>)
 >Re: nstask with admin privs (From: Michael Watson <email@hidden>)

  • Prev by Date: Re: Activating applications
  • Next by Date: Illegal NSTableView data source
  • Previous by thread: Re: nstask with admin privs
  • Next by thread: forwardInvocation: and keeping compiler happy
  • Index(es):
    • Date
    • Thread