I have made a function to check the user's password but I got a strange error when trying to run it:
*** glibc detected *** malloc(): memory corruption: 0x0805b280 ***
Aborted
here is the function:
Code:
int auth_passwd(const char *user, const char *password)
{
char *sys_pass, *user_pass, *salt;
struct spwd *sp;
sp=getspnam(user);
sys_pass=strdup(sp->sp_pwdp);
if(sys_pass==NULL) return -1;
if(!strncmp(sys_pass, "$1", 2)) {
/* the password is encrypted with MD5 algorithm */
if((salt=malloc(12))==NULL) {
perror("malloc");
exit(1);
}
strncpy(salt, sys_pass, 11);
salt[12]='\0';
} else {
/* the password is encrypted with DES algorithm */
if((salt=malloc(3))==NULL) {
perror("malloc");
exit(1);
}
strncpy(salt, sys_pass, 2);
salt[3]='\0';
}
user_pass=(char *)crypt(password, salt);
if(!strcmp(sys_pass, user_pass)) {
xfree(salt);
return=1;
} else {
xfree(salt);
result -1;
}
}
Can you tell me where is the problem ?