DB.pm 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Terry Weissman <terry@mozilla.org>
#                 Dan Mosedale <dmose@mozilla.org>
#                 Joe Robins <jmrobins@tgix.com>
#                 Dave Miller <justdave@syndicomm.com>
#                 Christopher Aillon <christopher@aillon.com>
#                 Gervase Markham <gerv@gerv.net>
#                 Christian Reis <kiko@async.com.br>
#                 Bradley Baetz <bbaetz@acm.org>
28
#                 Erik Stambaugh <erik@dasbistro.com>
29

30
package Bugzilla::Auth::Verify::DB;
31 32 33 34 35 36

use strict;

use Bugzilla::Config;
use Bugzilla::Constants;
use Bugzilla::Util;
37
use Bugzilla::User;
38

39 40 41 42 43 44 45 46 47 48 49 50
my $edit_options = {
    'new' => 1,
    'userid' => 0,
    'login_name' => 1,
    'realname' => 1,
};

sub can_edit {
    my ($class, $type) = @_;
    return $edit_options->{$type};
}

51 52 53 54 55
sub authenticate {
    my ($class, $username, $passwd) = @_;

    return (AUTH_NODATA) unless defined $username && defined $passwd;

56 57
    my $userid = Bugzilla::User::login_to_id($username);
    return (AUTH_LOGINFAILED) unless $userid;
58

59 60
    return (AUTH_LOGINFAILED, $userid) 
        unless $class->check_password($userid, $passwd);
61

62 63
    # The user's credentials are okay, so delete any outstanding
    # password tokens they may have generated.
64 65
    require Bugzilla::Token;
    Bugzilla::Token::DeletePasswordTokens($userid, "user_logged_in");
66

67 68
    # Account may have been disabled
    my $disabledtext = $class->get_disabled($userid);
69 70 71 72 73 74
    return (AUTH_DISABLED, $userid, $disabledtext)
      if $disabledtext ne '';

    return (AUTH_OK, $userid);
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
sub get_disabled {
    my ($class, $userid) = @_;
    my $dbh = Bugzilla->dbh;
    my $sth = $dbh->prepare_cached("SELECT disabledtext FROM profiles " .
                                   "WHERE userid=?");
    my ($text) = $dbh->selectrow_array($sth, undef, $userid);
    return $text;
}

sub check_password {
    my ($class, $userid, $passwd) = @_;
    my $dbh = Bugzilla->dbh;
    my $sth = $dbh->prepare_cached("SELECT cryptpassword FROM profiles " .
                                   "WHERE userid=?");
    my ($realcryptpwd) = $dbh->selectrow_array($sth, undef, $userid);

    # Get the salt from the user's crypted password.
    my $salt = $realcryptpwd;

    # Using the salt, crypt the password the user entered.
    my $enteredCryptedPassword = crypt($passwd, $salt);

    return $enteredCryptedPassword eq $realcryptpwd;
}

sub change_password {
    my ($class, $userid, $password) = @_;
    my $dbh = Bugzilla->dbh;
103
    my $cryptpassword = bz_crypt($password);
104 105 106 107
    $dbh->do("UPDATE profiles SET cryptpassword = ? WHERE userid = ?", 
             undef, $cryptpassword, $userid);
}

108 109 110 111 112 113
1;

__END__

=head1 NAME

114
Bugzilla::Auth::Verify::DB - database authentication for Bugzilla
115 116 117 118 119 120 121 122 123 124

=head1 SUMMARY

This is an L<authentication module|Bugzilla::Auth/"AUTHENTICATION"> for
Bugzilla, which logs the user in using the password stored in the C<profiles>
table. This is the most commonly used authentication module.

=head1 SEE ALSO

L<Bugzilla::Auth>