#include #include #include int foo(void); int main(int argc, char **argv) { uid_t uid; uid=getuid(); foo(); /* uid=root */ if (-1 == setuid(0)) perror("setuid(0)"); else foo(); /* return to uid, but still being able to get back to root */ if (-1 == setuid(uid)) perror("setuid(uid)"); else foo(); /* drop privileges */ if (-1 == seteuid(uid)) perror("seteuid(uid)"); else foo(); /* this will fail */ if (-1 == setuid(0)) perror("setuid(0)"); else foo(); } int foo(void) { #ifndef BS #define BS 512 #endif FILE *i; char buffer[BS]; size_t n; /* if ( NULL == ( i = popen( "/bin/id -a", "r")))*/ if ( NULL == ( i = popen( "/usr/xpg4/bin/id", "r"))) { perror("popen"); return 1; } while ( 0 != ( n = fread( buffer, 1, BS, i ))) fwrite( buffer, 1, n, stdout ); pclose(i); }