I'm trying to write a C program which is supposed to open a file which can only be read/written to by (non-root) User A. When run by users who are neither root nor User A, the program should allow the user to open the file with the effective user being User A.
Currently, my program is able to change euid only if it is run as sudo, which makes sense.
int main(int argc, char const *argv[]) {
// should be userB's uid
uid_t ruid = getuid();
uid_t fake_uid = <userA-uid>;
seteuid(fake_uid);
/* open, read, write to and close file here */
// returning euid to original uid
seteuid(ruid);
return 0;
}