我欲乘风yecheng.blog.chinaunix.net
yecheng_110
全部博文(78)
2007年(53)
2006年(25)
DT27
jolinok
ylke2007
saintdra
zhxd
大鬼不动
tntcheng
chenkeac
flb_2001
yyfq521
bluesky0
zjq5688
w1818618
168Kill
myoeoo
bolebdms
sxzf168
11qq22ww
分类: C/C++
2006-10-25 15:48:36
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <pwd.h> int main(int argc,char **argv) { pid_t pid; int status; struct passwd *pw; pw = getpwnam("nobody"); if(geteuid() != 0) { printf("You must be root!\n"); exit(1); } if(pw == NULL) { printf("Can't find user nobody\n"); exit(1); } if(setgid(pw->pw_gid) == -1 || setuid(pw->pw_uid) == -1) { printf("Unable to switch user %s(%d,%d)\n",pw->pw_name,pw->pw_uid, pw->pw_gid); exit(1); } if(setreuid(-1, 0) == 0) { printf("Unable to completely drop privileges\n"); exit(1); } if(geteuid() == 0) { printf("Running as root is NOT recommended"); } pid = fork(); if(pid < 0) { return -1; } else if(pid == 0) { execl("/bin/sh", "sh", "-c","ls", NULL); fprintf(stderr,"Something is wrong!\n"); } waitpid(-1,&status,0); if(WIFEXITED(status) == 0) { printf("Exit abnormity\n"); return -1; } printf("Exit status:%d\n",WEXITSTATUS(status) );
return 0; }
上一篇:Perl 安全[perl]
下一篇:fork两次避免产生僵尸进程[c/c++]
登录 注册