acidprime
acidprime

Reputation: 279

Simple? argv[] conversion to char*

So I have a sample application source I am trying to modify for my own purposes

it basically is running a shell script (test example below) but with privs using apple's security framework (see link below for original source).

#!/bin/bash
echo $@
whoami

In that sample code we pass the following:

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

I have modified the command to add myToolPath as the first argument as so:

        // Grab the arguent length
        int size = strlen(argv[1]);

        // Grab the first argument
        char myToolPath[size];
        strcpy(myToolPath,argv[1]);

which works, it means when I run

./compiled_priv_tool /tmp/example.sh

It returns "root" rather then the user I am using.

But now I want to be able to pass arguments to this helper and have them added to this array This is what the example does in the sample code

        char *myArguments[] = { "-u", NULL };

So my previous shell example would have output like this:

./compiled_priv_tool /tmp/example.sh

-u
root

but I would like to be able to dynamically pass as many arguments as argv can contain to this script so the output would look like this

./compiled_priv_tool /tmp/example.sh -u user -f file path -b lah 

-u user -f file path -b lah
root

I don't need to handle the arguments in the C code, I just need them to be passed directly to the shell script ( external command ).

So heres my (hopefully) simple question, if one wanted to take argv[] and copy it except for argument 0 (which is the path to ./compiled_priv) to something expecting the char * myArguments var what would that look like in code. I learned objective C and never started with C so this is probably pretty basic but I have tried a for loop and copying the two indexes, but its not working. something like:

char *myArguments[] = argv

is what i want but that does not work, as it says invalid initializer, and even if it did it looks like we need a NULL at the end of the array much like a NSTask. I would include my for loop but its a non starter.

So I need to the contents of argv from 1 to the end of the array and to add a NULL to the end and wrap that up in the same form as char *myArguments[] in the original sample code. If I understood c better it would help but in the mean time any thoughts?

Here is a link to the source I am playing with if it helps,

http://developer.apple.com/library/mac/documentation/security/conceptual/authorization_concepts/03authtasks/authtasks.html#//apple_ref/doc/uid/TP30000995-CH206-TPXREF33

Upvotes: 0

Views: 1127

Answers (1)

caf
caf

Reputation: 239051

argv is already in the format you require (a NULL terminated array of char * pointers), so you can just do:

char **myArguments = &argv[2];

(Assuming that you have already tested that argc is at least 2).

Upvotes: 2

Related Questions