LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   check Linux password from /etc/shadow (https://www.linuxquestions.org/questions/programming-9/check-linux-password-from-etc-shadow-680104/)

htabesh 10-30-2008 04:37 PM

check Linux password from /etc/shadow
 
Hi,
I want to write a C Program that get a password and user name, then compare it with encrypted password in /etc/shadow.
Please help me with SIMPLE CODES.

chrism01 10-30-2008 06:28 PM

Have a look here:
man crypt

bgeddy 10-30-2008 08:42 PM

There's some simple examples here

htabesh 10-31-2008 04:49 AM

I try below program:
Code:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>

int main (void)
{
        char *passwd;
        passwd = crypt("123456", "$1$yfKCUYj6");

}

And then compile that:

Code:

gcc 1.c -o 1
The out put of compile is:
Code:

/tmp/cci2MSHh.o: In function `main':
1.c:(.text+0x21): undefined reference to `crypt'
collect2: ld returned 1 exit status

What is the problem? :(

keefaz 10-31-2008 08:20 AM

try add -lcrypt in the gcc command line

htabesh 10-31-2008 08:55 AM

OK, thanks.
Now how can I write a C Program that get a password and user name, then compare it with encrypted password in /etc/shadow for authentication?

nishamathew1980 11-02-2008 04:58 AM

@ bgeddy,
That was an interesting and informative article. Thanks for posting it here.

Linux Archive

htabesh 11-04-2008 08:12 AM

Finally I found the way. :)
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;
        }
}

you can find and replace salt ("pass" in this program) in /etc/shadow for any user.
e.g.:
root:$1$Og9RbNrT$/gNprNaDgv.hNS01Ue2gi1:14159:0:99999:7:::

Keith Hedger 05-24-2013 12:44 PM

Thanks for this I've been trying a number of different example and this is the first that worked.
P.S. one minor typo "puts("Access granted"\n);" should be puts("Access granted\n");


All times are GMT -5. The time now is 04:30 AM.