LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 04: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, 06:28 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Have a look here:
man crypt
 
Old 10-30-2008, 08: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: 232Reputation: 232Reputation: 232
There's some simple examples here
 
Old 10-31-2008, 04: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, 08:20 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
try add -lcrypt in the gcc command line
 
Old 10-31-2008, 08: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, 04: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 04:49 AM.
 
Old 11-04-2008, 08: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 08:19 AM.
 
1 members found this post helpful.
Old 05-24-2013, 12:44 PM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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



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 12:17 PM
Shadow Password linuxjamil Linux - Server 1 08-04-2007 05:13 AM
shadow password - password field ayhopkins Linux - Security 8 11-17-2005 05:25 AM
Shadow password encryption mnisski Linux - General 3 05-28-2004 06:24 PM
shadow password wincrk Linux - Security 3 03-16-2003 09:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

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
Open Source Consulting | Domain Registration