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

hi, i am working on a project in which i need to accept user id and password from a html form(whihc should be same as the unix id and password for that person) and process that to check if that password is correct. is there are a way i can do it using a perl script, if yes, can anyone suggest me, how? Regrads Arti
  • Comment on user authentication using unix password file

Replies are listed 'Best First'.
Re: user authentication using unix password file
by wog (Curate) on Jun 08, 2001 at 02:32 UTC
    Note that crypt and getpw* won't work because some systems store passwords in /etc/shadow, and/or use MD5 to encrypt them (and the crypt function doesn't behave accordingly). Sometimes passwords might be managed off machine.

    You'll find the Authen::PAM module helpful if your system supports PAM (Pluggable Authentication Modules). Otherwise, Crypt::PasswdMD5 might be useful, as well as looking up the documentation for what fields are in /etc/shadow.

    If your system uses shadow passwords it is likely that your script will need to have root access. Regardless, you have a serious security issue if people can try to brute-force passwords on your system through your script.

      Update: On rereading, I've decided that mine isn't a very useful reply. /:

      Actually, even on many systems with shadowed passwords, either the C library's getpw*() or Perl's wrapper will grab the shadow encrypted password when the routine is called by a process with effective user ID of 0 (root).

              - tye (but my friends call me "Tye")
Re: user authentication using unix password file
by tadman (Prior) on Jun 08, 2001 at 04:32 UTC
    When I submitted a patch for Apache's auth module to make it work with /etc/passwd, they politely declined it. Apparently, they had received dozens of submissions like this, but were against it in principle. Using "system" passwords for Web-based management is a capital-B Bad Idea, for security reasons, and many others.

    Now, this was some time ago, back when /etc/passwd actually had "passwords" in it. Most systems now use /etc/shadow to store the encrypted passwords, which means that the CGI user cannot actually read this information. Otherwise, you would have to run your CGI as root, which is an Exceptionally Bad Idea.

    As wog was kind enough to mention, PAM might give you the access you require, and I would certainly look into using this.

    Remember, though, that disclosing your system password is highly dangerous. If, for some reason, a user's password were captured, and this user happened to have "sudo" access (a utility to run root-level commands and/or a root shell), then you are exposing yourself to a world of hurt. All it would take is one of your admins to log in to your Web page from a cable modem connection which happened to have a "sniffer" on it, and your system would be, as they say, chown'd. As in, no longer yours.

    Perhaps if you could describe the nature of your application, some ways of addressing this particular requirement could be investigated.
Re: user authentication using unix password file
by merlyn (Sage) on Jun 08, 2001 at 17:09 UTC
    It's generally considered a Bad Idea to do this. You'll have Unix passwords flying over the wire in the clear, making them easy to sniff. Of course, if you allow telnet or unencrypted FTP to the box, there's probably no greater threat anyway, but if you have telnet turned off and ssh turned on for security purposes (like most people do these days), then you've just reintroduced a hole.

    -- Randal L. Schwartz, Perl hacker

Re: user authentication using unix password file
by marcink (Monk) on Jun 08, 2001 at 02:24 UTC
    Assuming your machine is using crypt: yes, it's easy though on most systems you'd need /etc/shadow, which usually is readable only by root.

    Anyway, the code:

    #!/usr/bin/perl -w use strict; my $crypted_passwd = 'SqL9sEYFbjX0U'; my $clear_passwd = 'blah'; my $seed = substr( $crypted_passwd, 0, 2 ); print "It's correct\n" if crypt( $clear_passwd, $seed ) eq $crypted_pa +sswd;

    Update: Oops, misunderstood your question. Seems like serious caffeine shortage.

    The proper answer would probably be: use Authen::PAM ;-)

    -mk
Re: user authentication using unix password file
by sierrathedog04 (Hermit) on Jun 08, 2001 at 06:56 UTC
    If you are using Apache then the htCheckPassword command in the htpasswd module at CPAN enables you to check whether an Apache password is correct. In general, however, the Apache password and the Unix password will not be the same.

    It is possible to configure Apache so that it uses Unix Ids and passwords for authentication, but most experts strongly recommend against doing so.

    So the answer is yes, it is at least possible.

    # Check that a password is correct $pwdFile->htCheckPassword("zog", "password");
Re: user authentication using unix password file
by princepawn (Parson) on Jun 08, 2001 at 02:21 UTC
    "Writing Apache Modules with Perl and C" covers this. Authored by Lincoln Stein and Doug Maceachern.