| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: AuthenWE.pm,v 1.9 2005/03/14 10:15:30 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | ||
| 8 | # Copyright (C) 2002 Slaven Rezic. | ||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||
| 11 | |||
| 12 | # | ||
| 13 | # Mail: slaven@rezic.de | ||
| 14 | # WWW: http://we-framework.sourceforge.net | ||
| 15 | # | ||
| 16 | |||
| 17 | package Apache::AuthenWE; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use Apache::Constants ':common'; | ||
| 21 | $Apache::AuthenWE::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/); | ||
| 22 | |||
| 23 | sub handler { | ||
| 24 | |||
| 25 | require WE::DB; | ||
| 26 | |||
| 27 | my $r = shift; | ||
| 28 | my($res, $sent_pwd) = $r->get_basic_auth_pw; | ||
| 29 | return $res if $res; #decline if not Basic | ||
| 30 | |||
| 31 | my $name = $r->connection->user; | ||
| 32 | |||
| 33 | if ($name eq "") { | ||
| 34 | $r->note_basic_auth_failure; | ||
| 35 | $r->log_reason("Apache::AuthenWE - no username given", $r->uri); | ||
| 36 | return AUTH_REQUIRED; | ||
| 37 | } | ||
| 38 | |||
| 39 | # load apache config vars | ||
| 40 | my $dir_config = $r->dir_config; | ||
| 41 | |||
| 42 | my %args; | ||
| 43 | my $rootclass = $dir_config->get('WE_RootClass'); | ||
| 44 | my $rootdir = $dir_config->get('WE_RootDir'); | ||
| 45 | my $logoutuser = $dir_config->get('WE_Authen_LogoutUser'); | ||
| 46 | my $ignoreuser = $dir_config->get('WE_Authen_IgnoreUser'); | ||
| 47 | |||
| 48 | if (defined $logoutuser && $logoutuser ne "") { | ||
| 49 | return OK if $logoutuser eq $name; | ||
| 50 | } | ||
| 51 | |||
| 52 | if (defined $ignoreuser && $ignoreuser ne "") { | ||
| 53 | $r->note_basic_auth_failure; | ||
| 54 | return AUTH_REQUIRED if $ignoreuser eq $name; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (defined $rootclass) { $args{-class} = $rootclass } | ||
| 58 | if (defined $rootdir) { $args{-rootdir} = $rootdir } | ||
| 59 | $args{-readonly} = 1; | ||
| 60 | $args{-locking} = 0; | ||
| 61 | |||
| 62 | my $rootdb; | ||
| 63 | eval { | ||
| 64 | $rootdb = WE::DB->new(%args); | ||
| 65 | if (!$rootdb) { | ||
| 66 | die "Can't get db with args %args"; | ||
| 67 | } | ||
| 68 | if (!$rootdb->UserDB) { | ||
| 69 | die "No user database"; | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | if ($@) { | ||
| 73 | $r->note_basic_auth_failure; | ||
| 74 | $r->log_reason("Apache::AuthenWE - $@", $r->uri); | ||
| 75 | return AUTH_REQUIRED; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $identified; | ||
| 79 | eval { | ||
| 80 | $identified = $rootdb->identify($name, $sent_pwd); | ||
| 81 | }; | ||
| 82 | if ($@ || !$identified) { | ||
| 83 | $r->note_basic_auth_failure; | ||
| 84 | my $msg = "Apache::AuthenWE - Can't identify as $name"; | ||
| 85 | if ($@) { | ||
| 86 | $msg .= " - $@"; | ||
| 87 | } | ||
| 88 | $r->log_reason($msg, $r->uri); | ||
| 89 | return AUTH_REQUIRED; | ||
| 90 | } | ||
| 91 | |||
| 92 | my $requires = $r->requires; | ||
| 93 | my $userdb = $rootdb->UserDB; | ||
| 94 | for my $req (@$requires) { | ||
| 95 | my($require, @list) = split /\s+/, $req->{requirement}; | ||
| 96 | |||
| 97 | #ok if user is one of these users | ||
| 98 | if ($require eq "user") { | ||
| 99 | return OK if grep $name eq $_, @list; | ||
| 100 | } | ||
| 101 | #ok if user is simply authenticated | ||
| 102 | elsif ($require eq "valid-user") { | ||
| 103 | return OK; | ||
| 104 | } | ||
| 105 | elsif ($require eq "group") { | ||
| 106 | foreach my $group (@list) { | ||
| 107 | if ($userdb->is_in_group($rootdb->CurrentUser, $group)) { | ||
| 108 | return OK; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | $r->note_basic_auth_failure; | ||
| 115 | $r->log_reason("Apache::AuthenWE - user authentified, but not in require list", $r->uri); | ||
| 116 | return AUTH_REQUIRED; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | __END__ |