Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

/usr/bin/passwd

by Anonymous Monk
on Aug 02, 2000 at 23:36 UTC ( [id://25829]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a way to get a perl script to change a users shell password? I tried the following code, but it doesnt work the way its supposed to.
open ADD, '|/usr/bin/passwd $username'; sleep 1; print ADD $password . "\n"; sleep 1; print ADD $password . "\n"; sleep 1; close ADD;

Replies are listed 'Best First'.
Re: /usr/bin/passwd
by plaid (Chaplain) on Aug 02, 2000 at 23:59 UTC
    You probably want to take a look at the Expect module, which will let you handle the program as if you were typing it from the command line. Generally, sleeping like that isn't a safe way to go, as you have no idea whether the program is ready for input or not, and no way of verifying that the input it's prompting for is the input you're about to give it. Expect will let you wait for a certain prompt, such as (on my box)
    (current) UNIX password:
    Alternatively, you could take a look at IO::Tty, and use it to set up a pty, which will make the passwd program behave as if you were sitting at a terminal, but this is basically what Expect is doing behind the scenes.

    The problem you were running into may be related to OS buffering issues. You might try something like (code taken from Programming Perl):

    select((select(ADD), $| = 1)[0]);
    which would turn on autoflushing on the ADD filehandle. I'd still recommend using Expect though, as it provides a much more robust and flexible way to accomplish your goal.

    Update: I should have looked around a bit for this first, but you also might want to take a look at Unix::PasswdFile and/or Passwd::Solaris.

RE: /usr/bin/passwd
by ferrency (Deacon) on Aug 03, 2000 at 00:19 UTC
    If you're going to use the built in passwd command, you probably will need to go with something like expect, as plaid stated.

    But depending on what flavor of unix you're using, there may be another option which would lend itself to batch processing more easily than the passwd command. On FreeBSD (I'm not sure about other unices), you might want to check out the commands pw and chpass. They let you do various password file munging operations completely from the command line. You'll need to encrypt the password manually, unfortunately, but that's not too tricky: perldoc -f crypt for more information.

    Another option, which I personally prefer not to use, would be to have perl change /etc/passwd and /etc/master.passwd manually, and then rebuild the password databases with pwd_mkdb. That's how we used to do things in the olden days, but you need to be very careful about things like file locking, or you discover There's More Than One Way To Shoot Yourself In The Foot.

    Alan

    Update: Oops, apparently it's still the Olden Days around here. I'll have to tell The Other Guy about chpass. Not that I don't trust his code, but why reinvent the wheel?

Re: /usr/bin/passwd
by Anonymous Monk on Aug 03, 2000 at 03:42 UTC
    you could do something prompt them for a password, grab the salt from the encrypted password in the shadow file, use the crypt function with that salt, if it matches, prompt for the new password, get a random salt and use the crypt() function to create the new encrypted password, then write it to the shadow file.
Re: /usr/bin/passwd
by Merlin83 (Novice) on Aug 03, 2000 at 01:05 UTC
    Take a look at Brink, available from freshmeat. It's a Perl CGI script that changes users passwords from a web page, but the guts of the code would probably do what you want.
      yah iv seen it, but I am kinda scared to trust my perl script to deal directly with the password file which contains. 100's of users.
        Brink actually does all it's work on a copy of the passwd (actually shadow) file, but I guess it's still working on the file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://25829]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found