C - Real UID and Effective UID

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

int main(int argc, char *argv[]) {

    int err, uid, euid, fdgregglain, fdglainjr;

    char *prog = argv[0];

    uid = getuid();         /* get real UID */
    euid = geteuid();       /* get effective UID */
    printf("%s: uid = %d, euid = %d\n", prog, uid, euid);

    if (euid == 0) 
        printf("%s running as root\n", prog);

    err = setuid(uid);
    if (err)
        printf("Failed to switch to %d\n", uid);
    printf("Switched %s to run as %d\n", prog, geteuid());

    // Observation on OS X 10.9 - setuid away from root is a one-way street
    err = setuid(euid);
    if(err) 
        printf("Failed to switch to %d\n", euid);
    printf("Switched %s to run as %d\n", prog, geteuid());

}

Published on in