Name: Anonymous 2008-01-22 18:24
How would I make my program check if it was root?
#include <pwd.h>
my $username = getpwuid($<);
print "not " x ($username ne 'root'), "running as root\n";
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main () {
if ( geteuid() == 0 )
printf("You're root!\n");
else
printf("You're lower than a maggot!\n");
return 0;
}man 2 geteuid'.
extern struct passwd *getpwent (void);
#include <stdio.h>
#include <stdlib.h>
int main(void) {
if(strcmp("root", getenv("USER")) == 0)
printf("you are root\n");
else printf("you are not root\n");
return 0;
}
export USER=root' and, if the rest of the program is just as stupid, possibly cause some sort of security breach. It would also break with `su -p', `sudo' (with some sort of moronic configuration that keeps the value of $USER), or any number of programs that call `seteuid()' without updating the environment.
$ env USER=root ./\>\>10\'s\ shitty\ program
you are root
$#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
int main(){
puts(
strcmp(getpwuid(geteuid())->pw_name, "root") ?
"not running as root"
: "running as root"
);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
printf("I am%s root.\n", strcmp(*argv, "root") ? " not" : "");
return 0;
}