How can i check a string with actual user passwd (ie password in /etc/shadow)
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can i check a string with actual user passwd (ie password in /etc/shadow)
Hi all,
I am trying to write a remote access module. Is there any function in linux where I can give string (password entered by user) and compare it with the actual user password stored in /etc/shadow.
Since the password is stored encrypted in /etc/shadow I cannot parse and compare. So I want some method to compare if my user entered the correct password..Is there any function for that..
Tell us which lang you are using.
There should be some equiv of the crypt cmd http://linux.die.net/man/3/crypt and you need to know the salt by reading the entry in the shadow file. you can then invoke the 'crypt' cmd+salt (optionally using eg MD5) and generate an encrypted passwd that you then compare to the shadow entry.
Thanks chrism01 for the quick reply...I tried man page for both passwd and crypt...So you are saying that the password entered by my user should be encrypted using crypt() and then the resultant output string should be compared with the encrypted password in /etc/shadow file...
How can I determing which key the OS used to encrypt the password..So I must use the same key so that I can compare my password with the actual password...
I still does not fully understood that crypt function except I must use the same key that the OS used to obtain same level of encryption...
Please help me...
Using that reference I just tried this program but failed
Code:
#include <shadow.h>
//#include <shadow/pwauth.h>
#include <rpcsvc/yppasswd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
struct passwd *pw;
char entpw[30],*encpw;
char user[30];
printf("Enter which user : ");
scanf("%s",user);
printf("Enter the password : ");
scanf("%s",entpw);
if((pw=getpwnam(user))==NULL)
{
printf("No such user\n");
return -1;
}
if(strlen(pw->pw_passwd)==0)
{
printf("Welcome you can enter without passwd\n");
return 0;
}
encpw=(char *)crypt(entpw,pw->pw_passwd);
printf("Encrypted passwd is : %s\n",encpw);
if(!strcmp(encpw,pw->pw_passwd))
{
printf("You are always welcome\n");
return 0;
}
else
{
printf("Wrong passwd...hacking?\n");
return -1;
}
return 0;
}
I think I failed because crypt() function only works with DES encryption algorithm...I think in my OS (Centos 5) MD5 algorithm is used. I recognised this from shadow(5) man page.
Quote:
Optionally a password entry can start with a "$" character. This means the encrypted password was generated using another (not DES) algorithm. For example if it starts with "$1$" it means the MD5-based algorithm
was used
Unfortunately my OS uses MD5 algorithm..Is there is any function like crypt which encrypt string using MD5 algorithm..Caught up in a new roadblock...Please help me...
$1$<string>$", where "<string>" stands for the up to 8 characters following "$1$" in the salt,
so in this example, 'b1yv9grF' is the salt.
2. http://www.perlmonks.org/?node_id=198592 (same last post) gives 2 ways to create a salted md5 passwd to compare. As he says, your system will probably automatically do the md5 thing, so use the 2nd example.
You can try the first example as well if the 2nd one doesn't work.
The string which is the encrypted password in /etc/shadow can also be used as the key argument in crypt() function which will encrypt the string ...This program shows that (also from the same thread)
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
int main(int argc, char *agrv[])
{
const char *const pass = "$1$Og9RbNrT$/gNprNaDgv.hNS01Ue2gi1";
char *result;
int ok;
result = crypt(getpass("Password:"), pass);
ok = strcmp (result, pass);
if ( ok == 0 )
{
puts("Access granted"\n);
return 0;
}
else
{
puts ("Access denied\n");
return 1;
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.