LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-27-2008, 02:54 PM   #16
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17

According to the article, it says we can use the crypt function also. The only disadvantage is modern systems can break traditional DES passwords very quickly(according to the article.)

I have tried the crypt() with thw code

Quote:
sr/bin/perl -w

use strict;
my @line;
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

while(@line = <FILEHANDLE>){

my $newpass = 123;
my $pass = crypt($newpass,"password");
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");


}

close(FILEHANDLE
if this works, that means crypt can be used. sure there must be a way, the password can be retrieve from the file.txt and use it in the system("sudo /usr/sbin/useradd -m -p $pass $line[0]");

any ideas?
 
Old 03-27-2008, 03:07 PM   #17
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You HAVE to use the method your operating system uses. If your system uses MD5 passwords, you have to use the Crypt::PasswdMD5 module. If it uses DES, you have to use the standard crypt. I haven't seen an operating system that was installed in many years that used DES.

HTH

Forrest
 
Old 03-27-2008, 03:14 PM   #18
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
oic, but why, if i tried that code and it was succesfully added a user and then it can be login to tat user also. That means, modern operating system can still use the DES also. Correct me if I am wrong.
 
Old 03-27-2008, 03:20 PM   #19
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
If it is allowing you to log in with the password "123" then it is working, and you are using DES. This also means that your operating system is vulnerable to password attacks.

HTH

Forrest
 
Old 03-27-2008, 03:30 PM   #20
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
yea i guess ur right. But nobody uses my box. Im the only owner :P ,

Quote:
#!/usr/bin/perl -w

use strict;
my @line;
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

while(@line = <FILEHANDLE>){

my $pass = crypt($line[1],"password");
system("sudo /usr/sbin/useradd -m -p $pass $line[0]");

}

close(FILEHANDLE);
so how am i goin to add the password from the file.txt. How to solve this problem. my head is crushing.
 
Old 03-27-2008, 03:55 PM   #21
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
If you are sure you aren't using MD5 passwords, use this:

Code:
#!/usr/bin/perl -w

use strict;

# location of file storing usernames and passwords
my $inputFile = "file.txt";

# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );

# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

# read the file until all users are used
while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;
    chomp($username);
    chomp($password);

    # generate traditional (weak!) DES password
    my $passwd = crypt( $password, gensalt(2) );
    system( qq(sudo /usr/sbin/useradd -m -p '$passwd' $username) );
}

close(FILEHANDLE);

# uses global @salt to construct a RANDOM salt string of requested length
sub gensalt {
    my $count = shift;
    my $salt;
    for (1..$count) {
       $salt .= (@salt)[rand @salt];
    }
    return $salt;
}
If you aren't sure, run
Code:
sudo perl -MCPAN -e "install Crypt::PasswdMD5"
Then, the following will work (slightly different from last one):

Code:
#!/usr/bin/perl -w

use strict;
use Crypt::PasswdMD5 qw(unix_md5_crypt);

# change to 0 if not using MD5 passwords
my $useMD5 = 1;

# location of file storing usernames and passwords
my $inputFile = "file.txt";

# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );

# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

# read the file until all users are used
while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;
    chomp($username);
    chomp($password);

    my $passwd;

    if ($useMD5 == 1) {
        print "Using MD5";
        # generate modern md5 password
        $passwd = unix_md5_crypt( $password, gensalt(8) );
    } else {
        print "Using DES";
        # generate traditional (weak!) DES password
        $passwd = crypt( $password, gensalt(2) );
    }
    system( qq(sudo /usr/sbin/useradd -m -p '$passwd' $username) );
}

close(FILEHANDLE);

# uses global @salt to construct a RANDOM salt string of requested length
sub gensalt {
    my $count = shift;
    my $salt;
    for (1..$count) {
       $salt .= (@salt)[rand @salt];
    }
    return $salt;
}
HTH

Forrest

Last edited by forrestt; 03-27-2008 at 04:00 PM.
 
Old 03-27-2008, 04:15 PM   #22
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
OK, After the Crypt::PasswdMD5 module is installed as above, this will determine the correct password type for your system and then use that to make the users' accounts.

Code:
#!/usr/bin/perl -w

use strict;
use Crypt::PasswdMD5 qw(unix_md5_crypt);

# Location of Commands
my $head = "/usr/bin/head";
my $sudo = "/usr/bin/sudo";
my $useradd = "/usr/sbin/useradd";
my $awk = "/usr/bin/awk";

# location of file storing usernames and passwords

my $inputFile = "file.txt";

my $useMD5;

# Test to see what kind of password entries are being used
my $pwlength=`$sudo $head -1 /etc/shadow | $awk -F: '{print \$2}' |wc -c`;
if ($pwlength == 35) {
    $useMD5 = 1;
} else {
    $useMD5 = 0;
}

# Valid characters for a password salt
my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );

# open the user/password file
open(FILEHANDLE, "<file.txt") or die "cannot open file for reading: $!";

# read the file until all users are used
while(<FILEHANDLE>){
    my $username = $_;
    my $password = <FILEHANDLE>;
    chomp($username);
    chomp($password);

    my $passwd;

    if ($useMD5 == 1) {
        print "Using MD5\n";
        # generate modern md5 password
        $passwd = unix_md5_crypt( $password, gensalt(8) );
    } else {
        print "Using DES\n";
        # generate traditional (weak!) DES password
        $passwd = crypt( $password, gensalt(2) );
    }
    system( qq($sudo $useradd -m -p '$passwd' $username) );
}

close(FILEHANDLE);

# uses global @salt to construct a RANDOM salt string of requested length
sub gensalt {
    my $count = shift;
    my $salt;
    for (1..$count) {
       $salt .= (@salt)[rand @salt];
    }
    return $salt;
}
HTH

Forrest
 
Old 03-27-2008, 06:51 PM   #23
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, reading from a filehandle at the top of a loop and in the middle is not really recommended.
There are various ways, depending on the file layout and size e.g. for the file as described:
Code:
file.txt

user1
passwd1


code:

# Open file , grab recs, close it
open( REQ_FILE, "<$infile_name" ) or
                exit_with_error("Unable to open file: $infile_name: $!\n");
@file_recs = <REQ_FILE>;
chomp(@file_recs);      # remove newline chars
close(REQ_FILE) or
                exit_with_error("Unable to close file: $infile_name: $!\n");
# process array here
If you have a 'small' file like this:
Code:
user1,passwd1
user2,passwd2
you can use a similar technique:

Code:
# Open file , grab recs, close it
open( REQ_FILE, "<$infile_name" ) or
                exit_with_error("Unable to open file: $infile_name: $!\n");
@file_recs = <REQ_FILE>;
chomp(@file_recs);      # remove newline chars
close(REQ_FILE) or
                exit_with_error("Unable to close file: $infile_name: $!\n");
# process array here
for $rec ( @file_recs)
{
    ($user, $passwd) = split(/,/, $rec);
    # process values here
}
for a 'large' file :
Code:
open( REQ_FILE, "<$infile_name" ) or
                exit_with_error("Unable to open file: $infile_name: $!\n");
while ( defined ( $file_rec = <REQ_FILE> ) )
{
    chomp($file_rec);
    ($user, $passwd) = split(/,/, $file_rec);
    # process values here
}
close(REQ_FILE) or
                exit_with_error("Unable to close file: $infile_name: $!\n");
 
Old 03-27-2008, 08:59 PM   #24
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by chrism01 View Post
Actually, reading from a filehandle at the top of a loop and in the middle is not really recommended.
There are various ways, depending on the file layout and size e.g. for the file as described
You can also do something something for files like this:
Code:
user1
pass1

user2
pass2

user3
pass3

…
You can read paragraphs at a time like so:
Code:
$/='';

while(<FILEHANDLE>) {
	my ($user,$pass) = split /\n/;

	#do stuff
}
Anyway, I would recommend against piping to “sudo passwd” and such. Try to find something on CPAN which does this for you (in a more reliable way).

Last edited by osor; 03-27-2008 at 09:01 PM.
 
Old 03-27-2008, 10:37 PM   #25
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
forrestt..thanks for ur help man...using Crypt::PasswdMD5 module is better choice than crypt() ..
 
  


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
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Perl Script to add user to a group AgentRn007 Programming 4 06-21-2006 09:20 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Adding multiple user shell script plexus Programming 2 06-19-2004 08:36 PM
my adding user script seems weird and buggy nzx Programming 1 10-21-2003 07:29 AM

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

All times are GMT -5. The time now is 10:25 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