实现su命令

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <pwd.h>
#include <shadow.h>
#include <termios.h>

int main(int argc, char *argv[])
{
char *user = "root";

if(argv[1] != NULL)
{
user = argv[1];
}

//input  password
printf("Passwd: ");
fflush(stdout);
char passwd[128] = {0};
struct termios old, new;
tcgetattr(0, &old);
new = old;
new.c_lflag &= ~ECHO;
new.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &new);
//fgets(passwd, 128, stdin);
char c = 0;
int num = 0;
while((c = getchar())!= '\n')
{
if(c == 127)
{
if(num > 0)
{
passwd[--num] = 0;
printf("\033[1D");
printf("\033[K");
}
continue;
}
printf("*");
fflush(stdout);
passwd[num++] = c;
}
tcsetattr(0, TCSANOW, &old);
//passwd[strlen(passwd) - 1] = 0;

printf("\n");

struct spwd *sp = getspnam(user);
assert(sp != NULL);

//printf("%s\n", sp->sp_pwdp);

char salt[128] = {0};
int i = 0, count = 0;
for(; sp->sp_pwdp[i] != 0; ++i)
{
salt[i] = sp->sp_pwdp[i];
if(salt[i] == '$')
{
count++;
if(count == 3)
{
break;
}
}
}

char *p = crypt(passwd, salt);
assert(p != NULL);

//printf("%s\n", p);

if(strcmp(p, sp->sp_pwdp) != 0)
{
printf("passwd error\n");
exit(0);
}

pid_t pid = fork();
assert(pid != -1);

if(pid == 0)
{
struct passwd *pw = getpwnam(user);
assert(pw != NULL);
setuid(pw->pw_uid);
setenv("HOME", pw->pw_dir, 1);
execl(pw->pw_shell, pw->pw_shell,(char*)0);
printf("error\n");
exit(0);
}
else
{
wait(NULL);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值