LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-30-2008, 05:37 PM   #1
htabesh
LQ Newbie
 
Registered: May 2006
Location: Iran-Tehran
Distribution: Enterprise RedHat
Posts: 25

Rep: Reputation: 16
Question 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.
 
Old 10-30-2008, 07:28 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Have a look here:
man crypt
 
Old 10-30-2008, 09:42 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
There's some simple examples here
 
Old 10-31-2008, 05:49 AM   #4
htabesh
LQ Newbie
 
Registered: May 2006
Location: Iran-Tehran
Distribution: Enterprise RedHat
Posts: 25

Original Poster
Rep: Reputation: 16
Unhappy

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?
 
Old 10-31-2008, 09:20 AM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
try add -lcrypt in the gcc command line
 
Old 10-31-2008, 09:55 AM   #6
htabesh
LQ Newbie
 
Registered: May 2006
Location: Iran-Tehran
Distribution: Enterprise RedHat
Posts: 25

Original Poster
Rep: Reputation: 16
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?
 
Old 11-02-2008, 05:58 AM   #7
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

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

Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 05:49 AM.
 
Old 11-04-2008, 09:12 AM   #8
htabesh
LQ Newbie
 
Registered: May 2006
Location: Iran-Tehran
Distribution: Enterprise RedHat
Posts: 25

Original Poster
Rep: Reputation: 16
Lightbulb

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:::

Last edited by htabesh; 11-04-2008 at 09:19 AM.
 
1 members found this post helpful.
Old 05-24-2013, 01:44 PM   #9
Keith Hedger
Member
 
Registered: Jun 2010
Location: Devon,UK
Distribution: Linux From Scratch, Slackware64,
Posts: 993

Rep: Reputation: 214Reputation: 214Reputation: 214
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");
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
password expiration /etc/shadow itik Linux - Newbie 3 07-25-2008 01:17 PM
Shadow Password linuxjamil Linux - Server 1 08-04-2007 06:13 AM
shadow password - password field ayhopkins Linux - Security 8 11-17-2005 06:25 AM
Shadow password encryption mnisski Linux - General 3 05-28-2004 07:24 PM
shadow password wincrk Linux - Security 3 03-16-2003 10:07 PM


All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration