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

Hello All. Is there any way in Perl, in which you can match a user-password? That is as a user to enter a username and password and check them if they are correct. We are talking about an app with root access but no brute forcing or stuff like that. Thanks Sawan

Replies are listed 'Best First'.
Re: Passwords on unix
by robartes (Priest) on Feb 04, 2003 at 09:17 UTC
    Have a look at the documentation for crypt. It even specifies a way to do what you are asking:
    $pwd = (getpwuid($<))[1]; system "stty -echo"; print "Password: "; chomp($word = <STDIN>); print "\n"; system "stty echo"; if (crypt($word, $pwd) ne $pwd) { die "Sorry...\n"; } else { print "ok\n"; }
    This was lifted straight from perldoc -f crypt.

    CU
    Robartes-

Re: Passwords on unix
by TheHobbit (Pilgrim) on Feb 04, 2003 at 10:11 UTC

    Hi,
    Sorry, but the way proposed by robartes, while certanly a solution is not 'the right thing'.

    If you want to authenticate a user there are two aspects that must be adressed:

    • you must get the password without echoing it back
    • you must check the password agains the user password known to the system.

    The first problem depends on the kind of interface you use (GUI, command line...). I'll try to help you there if you say something more about your interface.

    the second problem is where the meat is. If, as it is the case of allmost all Unix installation these days, your system uses PAM, you could use the wonderfull modules avaible on CPAN Authen::PAM and Authen::SimplePam. The first is more powerfull, while the second, as the name suggests, is simpler and generally adapted to solve the kind of problems you have.

    If any problem should arise using these modules, feel free to ask again, I'll try to help as well as I can

    Hoping the help does some good...


    Leo TheHobbit
Re: Passwords on unix
by zentara (Cardinal) on Feb 04, 2003 at 16:21 UTC
    Well advice from TheHobbit is hard to beat. But in the interest of answering the question, try this code:
    #!/usr/bin/perl #Check password encryption $username = shift || die "Usage $0 username plaintext_password\n"; $plaintext = shift || die "Usage $0 username plaintext_password\n"; $encpass = (getpwnam($username))[1]; print "Testing encryption ... "; if (crypt($plaintext, $encpass) ne $encpass) { print "FAILED\nTrying MD5 encryption ... "; if ( eval "use Crypt::PasswdMD5" ) { print "FAILED\n\nCannot try MD5 since Crypt::PasswdMD5 is not inst +alled.\n"; } else { if (&unix_md5_crypt($plaintext, $encpass) ne $encpass) { print "FAILED\n\nUnable to automatically use passwords from your + system.\n"; } else { print "OK\n\nMD5 passwords were detected.\n"; } } } else { print "OK\n"; }
      That approach is going to fail on any system that uses shadow password files. Many modern Unices use shadow password files.

      Abigail

        Abiigail-II said::

        That approach is going to fail on any system that uses shadow password files.

        I don't know why. getpwent() and friends do the right thing even in systems with shadow passwords, provided you are superuser.

        Perl's crypt() will call whatever crypt() is there in the system's library. Crypt::Passwd and Crypt::PasswdMD5 will cover the gap if you want to use a file from a different (incompatible) crypted password, but this is not the case at hand..

        Best regards

        -lem, but some call me fokat

Re: Passwords on unix -clarification
by sawanv (Novice) on Feb 05, 2003 at 03:38 UTC
    Hello All. Sorry for some of the misunderstanding caused by my wronlgly worded question. Apologies all around. Yes, it is the system password I am after. Yes it will be from /etc/shadow. I can see the encrypted password...am going to try to see if it can decrypt with crypt(), as you guys suggested. My app needs root access becaus it needs to read from and make changes to another application which can aonly be done by root. I want to avoid installing any other modules as would prefer my App to worl with the standard Perl distribution. Thanks for your help and keep it flowing. Sawan
Re: Passwords on unix
by jacques (Priest) on Feb 04, 2003 at 14:46 UTC
    Is there any way in Perl, in which you can match a user-password? That is as a user to enter a username and password and check them if they are correct.

    You could simply define the username/password in your program, and see if it matches the ones the user submits. But this has nothing to do with Perl. You could do this in many other languages.

    We are talking about an app with root access but no brute forcing or stuff like that.

    Oh, so the password would be the password of a system account? Your question is poorly worded. Why does your application need root access? I would try to avoid that, if I were you. (And don't even think of putting the root password in the program, like I suggest above.) If you must use a system account, then I would create a special account, which only has privileges to accomplish what your application needs done.