| 1 | # | ||
| 2 | # UserDBAuthen.pm - Apache authentication handler the uses the UserDB. | ||
| 3 | # | ||
| 4 | # This file is part of Apache::iNcom. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms of the GNU General Public License as published by | ||
| 12 | # the Free Software Foundation; either version 2 of the License, or | ||
| 13 | # (at your option) any later version. | ||
| 14 | # | ||
| 15 | # This program is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | # GNU General Public License for more details. | ||
| 19 | # | ||
| 20 | # You should have received a copy of the GNU General Public License | ||
| 21 | # along with this program; if not, write to the Free Software | ||
| 22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 23 | # | ||
| 24 | package Apache::iNcom::UserDBAuthen; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | |||
| 28 | use Apache; | ||
| 29 | use Apache::Constants qw( :common :response ); | ||
| 30 | |||
| 31 | use Apache::iNcom; | ||
| 32 | |||
| 33 | use DBIx::SearchProfiles; | ||
| 34 | use DBIx::UserDB; | ||
| 35 | |||
| 36 | sub handler { | ||
| 37 | my $r = shift; | ||
| 38 | |||
| 39 | # Load iNcom configuration | ||
| 40 | |||
| 41 | # Setup the database object | ||
| 42 | my $sql_profile = $r->dir_config( "INCOM_SEARCH_PROFILE" ) | ||
| 43 | || "conf/search_profiles.pl"; | ||
| 44 | $sql_profile = $r->server_root_relative( $sql_profile ); | ||
| 45 | |||
| 46 | my $db = new DBIx::SearchProfiles( $r->pnotes( "INCOM_DBH" ), | ||
| 47 | $sql_profile ); | ||
| 48 | |||
| 49 | # Setup the UserDB object | ||
| 50 | my $userdb = new DBIx::UserDB( $db, | ||
| 51 | $r->dir_config( "INCOM_USERDB_PROFILE" ), | ||
| 52 | $r->dir_config( "INCOM_GROUPDB_PROFILE" ) | ||
| 53 | ); | ||
| 54 | |||
| 55 | my $use_session = $r->dir_config( "INCOM_AUTH_SESSION" ) || "false"; | ||
| 56 | $use_session = $use_session =~ /1|t(rue)?|on|y(es)?/i; | ||
| 57 | if ( $use_session ) { | ||
| 58 | |||
| 59 | # Try to authenticate with the session | ||
| 60 | my $session = $r->pnotes( "INCOM_SESSION" ); | ||
| 61 | if ( exists $session->{_incom_logged_in} ) { | ||
| 62 | |||
| 63 | my $user = $userdb->user_get( $session->{_incom_logged_in} ); | ||
| 64 | $r->connection->user( $user->{username} ); | ||
| 65 | return OK; | ||
| 66 | |||
| 67 | } else { | ||
| 68 | |||
| 69 | $r->note_basic_auth_failure; | ||
| 70 | $r->log_reason( "User is not logged in", $r->filename ); | ||
| 71 | return Apache::iNcom::return_error( $r, AUTH_REQUIRED ); | ||
| 72 | |||
| 73 | } | ||
| 74 | } else { | ||
| 75 | |||
| 76 | # Get user credentials | ||
| 77 | my ( $rc, $passwd ) = $r->get_basic_auth_pw(); | ||
| 78 | return $rc if $rc != OK; | ||
| 79 | |||
| 80 | # Get the username | ||
| 81 | my $username = $r->connection->user; | ||
| 82 | |||
| 83 | # We are missing some infos here | ||
| 84 | unless ( $username and $passwd ) { | ||
| 85 | $r->note_basic_auth_failure; | ||
| 86 | $r->log_reason( "Missing username or password for authentication", | ||
| 87 | $r->filename ); | ||
| 88 | return Apache::iNcom::return_error( $r, AUTH_REQUIRED ); | ||
| 89 | } | ||
| 90 | |||
| 91 | # Check authentication | ||
| 92 | if ( $userdb->user_login( $username, $passwd ) ) { | ||
| 93 | return OK; | ||
| 94 | } else { | ||
| 95 | $r->note_basic_auth_failure; | ||
| 96 | $r->log_reason( "Authentication failed for $username", | ||
| 97 | $r->filename ); | ||
| 98 | return Apache::iNcom::return_error( $r, AUTH_REQUIRED ); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ |