I am trying to make a python script executable with the setuid bit set. The program, belonging to user 'bgmc', must create some files in the directory '/home/bgmc', but is called by another user, 'client'. Indeed, I don't want user 'client' to change the files created by the program. I used a c-wrapper to call the program (see setuid on shell scripts):
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
setuid(0);
system("/home/bgmc/myprogram.sh");
return 0;
}
I set the setuid bit of the program on. When the c-compiled program belongs to root, the program runs well and creates the expected file. The properties of the c-compiled program are then:
8 -rws--x--x 1 root root 4657 Mar 2 16:25 myprogram
However, when I change the user-group of myprogram to bgmc:bgmc, the program cannot create the file anymore: "Permission denied". I tried to change the line:
setuid(0);
with:
setuid(1002);
since 1002 is the user id of 'bgmc' (I used command "id -u bgmc" for this) but this didn't help.
I would rather prefer not giving root access to the program. Is there a way to prevent this?