SUMMARY: Running program as a certain user

Joel Turoff (turoff@disaster.com)
Tue, 04 Nov 1997 16:44:29 +0000

Greetings!

Many thanks to all who responded to my question about how to run a program
as a certain user from an rc script.

The problem was that we had a program that needed to be invoked at system
startup time as the user "server".

Many responded indicating that the best way to achieve this is to su to the
user in the rc script and run the "start" command to fire up the program:

su - server -c "/opt/server/start"

Some pointed out that the dash following su was very important because it
gives you the user's environment.

Others suggested running the program setuid, but I forgot to mention that
the program I was trying to start was suid root, but needed to be invoked
as the user "server".

And John Birtley was nice enough to write a small C wrapper to do the job.
Here it is in case anyone else would like to try it out (thanks John!):

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

#define SERVER_UID <user id of 'server' user>

int main (it argc, char **argv)
{
if (setreuid (SERVER_UID) == 0)
{
/*
* setuid() call was successful. We are now running
* as the 'server' user
*/
execv ("/opt/server/start", "/opt/server/start", NULL);
}
else
{
/*
* The setuid() call failed
*/
perror ("setuid");
exit (1);
}
}

Compile this and then:
chown server <program-name>
chmod u+s <program name>

to make it a setuid executable.

Again, many thanks to all who responded.

Joel