Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting the passwd in perl

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
We are trying to set a user passwd in UNIX using the following code in perl. It does not work. How do you do this so the script does not hang waiting for input?

$result = system("passwd", "$username", "$passwd", "$passwd");
 
Ive encountered this before and I tried the Expect module. Try that ....its pretty straightforward.

HTH

regards
C


"Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
latch -- this question or one like it keeps on coming up - but not many people have experience with either expect or Expect.pm.

Do you have any sample code you'd be willing to post here?

Regards, Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
My machine crashed and Ive been trying to recover data from it for the past 2 days. Shall post the sample script very soon



regards
C "Brahmaiva satyam"
-Adi Shankara (788-820 AD)
 
Thx latch :) Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Hi,

As Mike guessed, I am not familiar with expect. Are there any suggestions for where I could see some sample code?

Thanks,
Angela
 
Couldn't you just use the crypt function and write directly to the passwd/shadow/group files?
 
Hi yauncin,

Thanks. I talked to a coworker about doing this. He said I would have to compile a c program that specifically calls the crypt function and than call it from within my perl script. It sounded very complicated to me.

What do you think? Have you done this before?

Thanks,
Angela

 
Perl has a crypt function that will encrypt a password into a 13 character string with the first two characters being the salt. I've used this to create encrypted passwords in some of my databases with the following pieces of code. (Requirements are that the password requested is passed into the script in some form or another. I commonly use web pages)

Code:
	$cryptsalt = (substr($passwd,0,1)) . (substr($passwd,-1,1));
	$password = crypt($passwd, $cryptsalt);

I love being difficult, so I use the first and last character of the user's password as my salt to make it a little more difficult to crack, but that's purely user preference.

This is from the Perlfunc for crypt for your reference as well.

Code:
crypt PLAINTEXT,SALT
            Encrypts a string exactly like the crypt(3) function in the C
            library (assuming that you actually have a version there that
            has not been extirpated as a potential munition). This can prove
            useful for checking the password file for lousy passwords,
            amongst other things. Only the guys wearing white hats should do
            this.

            Note that "crypt" is intended to be a one-way function, much
            like breaking eggs to make an omelette. There is no (known)
            corresponding decrypt function. As a result, this function isn't
            all that useful for cryptography. (For that, see your nearby
            CPAN mirror.)

            When verifying an existing encrypted string you should use the
            encrypted text as the salt (like "crypt($plain, $crypted) eq
            $crypted"). This allows your code to work with the standard
            "crypt" and with more exotic implementations. When choosing a
            new salt create a random two character string whose characters
            come from the set "[./0-9A-Za-z]" (like "join '', ('.', '/',
            0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]").

            Here's an example that makes sure that whoever runs this program
            knows their own password:

                $pwd = (getpwuid($<))[1];

                system &quot;stty -echo&quot;;
                print &quot;Password: &quot;;
                chomp($word = <STDIN>);
                print &quot;\n&quot;;
                system &quot;stty echo&quot;;

                if (crypt($word, $pwd) ne $pwd) {
                    die &quot;Sorry...\n&quot;;
                } else {
                    print &quot;ok\n&quot;;
                }

            Of course, typing in your own password to whoever asks you for
            it is unwise.

            The the crypt manpage function is unsuitable for encrypting
            large quantities of data, not least of all because you can't get
            the information back. Look at the by-module/Crypt and
            by-module/PGP directories on your favorite CPAN mirror for a
            slew of potentially useful modules.

Hope this helps!

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top