Thursday, March 01, 2007

setuid wrapper

I needed to run a script with root privileges,but had
forgotten that Solaris (and I guess mosts versions of Unix) will not allow setuid on scripts. The solutions is to write a binary wrapper will will call the script:


#include <unistd.h>
#include <stdio.h>

#define myfile "/path/to/script"

main(argc, argv)
char **argv;
{
setuid(0);
seteuid(0);
execv(myfile, argv);
}


2 comments:

David Pashley said...

You probably really want to clean up the environment before execing the command.

Anonymous said...

Thank you for the code.
After compile, be sure to chmod 4711 pgm name.
Othewise, it may not work.