- 论坛徽章:
- 0
|
#include
#include
char *get_file_data(const char *file_path);
char *getshadowname(const char *name);
int file_size(const FILE *fp)
{
int file_size;
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return file_size;
}
main()
{
char *encrypted, *correct;
printf("%s\n", (correct =getshadowname("xxx")));
encrypted = crypt("xxxxx", correct);
if (!strcmp(encrypted, correct))
printf("login success!\n");
else
printf("login failed!\n");
}
char *get_file_data(const char *file_path)
{
FILE *fp;
int f_size;
int file_size(const FILE *fp);
char *file_data;
int id=0;
fp = fopen(file_path, "r");
f_size = file_size(fp);
file_data = (char *)malloc(f_size);
while((file_data[id++] = fgetc(fp)) != EOF);
file_data[id-1]='\0';
fclose(fp);
return file_data;
}
char *getshadowname(const char* name)
{
char *file_data;
char *p1, *p2, *p3, *p4;
char *value;
int value_size;
int i;
file_data = get_file_data("/etc/shadow");
p1 = strstr(file_data, name);
p2 = strtok(p1, ":");
p3 = strtok(NULL, ":");
p4 = strtok(NULL, ":");
value_size = p4 - p3 - 1;
value = (char *)malloc(value_size + 1);
for(i = 0; i
编译时要加入 -lcrypt 参数
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5421/showart_328582.html |
|