Chris@0: package Apache::Authn::Redmine;
Chris@0:
Chris@0: =head1 Apache::Authn::Redmine
Chris@0:
Chris@0: Redmine - a mod_perl module to authenticate webdav subversion users
Chris@0: against redmine database
Chris@0:
Chris@0: =head1 SYNOPSIS
Chris@0:
Chris@0: This module allow anonymous users to browse public project and
Chris@0: registred users to browse and commit their project. Authentication is
Chris@0: done against the redmine database or the LDAP configured in redmine.
Chris@0:
Chris@0: This method is far simpler than the one with pam_* and works with all
Chris@0: database without an hassle but you need to have apache/mod_perl on the
Chris@0: svn server.
Chris@0:
Chris@0: =head1 INSTALLATION
Chris@0:
Chris@0: For this to automagically work, you need to have a recent reposman.rb
Chris@0: (after r860) and if you already use reposman, read the last section to
Chris@0: migrate.
Chris@0:
Chris@0: Sorry ruby users but you need some perl modules, at least mod_perl2,
Chris@0: DBI and DBD::mysql (or the DBD driver for you database as it should
Chris@0: work on allmost all databases).
Chris@0:
Chris@0: On debian/ubuntu you must do :
Chris@0:
Chris@0: aptitude install libapache-dbi-perl libapache2-mod-perl2 libdbd-mysql-perl
Chris@0:
Chris@0: If your Redmine users use LDAP authentication, you will also need
Chris@0: Authen::Simple::LDAP (and IO::Socket::SSL if LDAPS is used):
Chris@0:
Chris@0: aptitude install libauthen-simple-ldap-perl libio-socket-ssl-perl
Chris@0:
Chris@0: =head1 CONFIGURATION
Chris@0:
Chris@0: ## This module has to be in your perl path
Chris@0: ## eg: /usr/lib/perl5/Apache/Authn/Redmine.pm
Chris@0: PerlLoadModule Apache::Authn::Redmine
Chris@0:
Chris@0: DAV svn
Chris@0: SVNParentPath "/var/svn"
Chris@0:
Chris@0: AuthType Basic
Chris@0: AuthName redmine
Chris@0: Require valid-user
Chris@0:
Chris@0: PerlAccessHandler Apache::Authn::Redmine::access_handler
Chris@0: PerlAuthenHandler Apache::Authn::Redmine::authen_handler
Chris@0:
Chris@0: ## for mysql
Chris@0: RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
Chris@0: ## for postgres
Chris@0: # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
Chris@0:
Chris@0: RedmineDbUser "redmine"
Chris@0: RedmineDbPass "password"
Chris@0: ## Optional where clause (fulltext search would be slow and
Chris@0: ## database dependant).
Chris@0: # RedmineDbWhereClause "and members.role_id IN (1,2)"
Chris@0: ## Optional credentials cache size
Chris@0: # RedmineCacheCredsMax 50
Chris@0:
Chris@0:
Chris@0: To be able to browse repository inside redmine, you must add something
Chris@0: like that :
Chris@0:
Chris@0:
Chris@0: DAV svn
Chris@0: SVNParentPath "/var/svn"
Chris@0: Order deny,allow
Chris@0: Deny from all
Chris@0: # only allow reading orders
Chris@0:
Chris@0: Allow from redmine.server.ip
Chris@0:
Chris@0:
Chris@0:
Chris@0: and you will have to use this reposman.rb command line to create repository :
Chris@0:
Chris@0: reposman.rb --redmine my.redmine.server --svn-dir /var/svn --owner www-data -u http://svn.server/svn-private/
Chris@0:
Chris@0: =head1 MIGRATION FROM OLDER RELEASES
Chris@0:
Chris@0: If you use an older reposman.rb (r860 or before), you need to change
Chris@0: rights on repositories to allow the apache user to read and write
Chris@0: S
Chris@0:
Chris@0: sudo chown -R www-data /var/svn/*
Chris@0: sudo chmod -R u+w /var/svn/*
Chris@0:
Chris@0: And you need to upgrade at least reposman.rb (after r860).
Chris@0:
Chris@0: =cut
Chris@0:
Chris@0: use strict;
Chris@0: use warnings FATAL => 'all', NONFATAL => 'redefine';
Chris@0:
Chris@0: use DBI;
Chris@0: use Digest::SHA1;
Chris@0: # optional module for LDAP authentication
Chris@0: my $CanUseLDAPAuth = eval("use Authen::Simple::LDAP; 1");
Chris@0:
Chris@0: use Apache2::Module;
Chris@0: use Apache2::Access;
Chris@0: use Apache2::ServerRec qw();
Chris@0: use Apache2::RequestRec qw();
Chris@0: use Apache2::RequestUtil qw();
Chris@0: use Apache2::Const qw(:common :override :cmd_how);
Chris@0: use APR::Pool ();
Chris@0: use APR::Table ();
Chris@0:
Chris@0: # use Apache2::Directive qw();
Chris@0:
Chris@0: my @directives = (
Chris@0: {
Chris@0: name => 'RedmineDSN',
Chris@0: req_override => OR_AUTHCFG,
Chris@0: args_how => TAKE1,
Chris@0: errmsg => 'Dsn in format used by Perl DBI. eg: "DBI:Pg:dbname=databasename;host=my.db.server"',
Chris@0: },
Chris@0: {
Chris@0: name => 'RedmineDbUser',
Chris@0: req_override => OR_AUTHCFG,
Chris@0: args_how => TAKE1,
Chris@0: },
Chris@0: {
Chris@0: name => 'RedmineDbPass',
Chris@0: req_override => OR_AUTHCFG,
Chris@0: args_how => TAKE1,
Chris@0: },
Chris@0: {
Chris@0: name => 'RedmineDbWhereClause',
Chris@0: req_override => OR_AUTHCFG,
Chris@0: args_how => TAKE1,
Chris@0: },
Chris@0: {
Chris@0: name => 'RedmineCacheCredsMax',
Chris@0: req_override => OR_AUTHCFG,
Chris@0: args_how => TAKE1,
Chris@0: errmsg => 'RedmineCacheCredsMax must be decimal number',
Chris@0: },
Chris@0: );
Chris@0:
Chris@0: sub RedmineDSN {
Chris@0: my ($self, $parms, $arg) = @_;
Chris@0: $self->{RedmineDSN} = $arg;
Chris@0: my $query = "SELECT
Chris@245: hashed_password, salt, auth_source_id, permissions
Chris@0: FROM members, projects, users, roles, member_roles
Chris@0: WHERE
Chris@0: projects.id=members.project_id
Chris@0: AND member_roles.member_id=members.id
Chris@0: AND users.id=members.user_id
Chris@0: AND roles.id=member_roles.role_id
Chris@0: AND users.status=1
Chris@0: AND login=?
Chris@0: AND identifier=? ";
Chris@0: $self->{RedmineQuery} = trim($query);
Chris@0: }
Chris@0:
Chris@0: sub RedmineDbUser { set_val('RedmineDbUser', @_); }
Chris@0: sub RedmineDbPass { set_val('RedmineDbPass', @_); }
Chris@0: sub RedmineDbWhereClause {
Chris@0: my ($self, $parms, $arg) = @_;
Chris@0: $self->{RedmineQuery} = trim($self->{RedmineQuery}.($arg ? $arg : "")." ");
Chris@0: }
Chris@0:
Chris@0: sub RedmineCacheCredsMax {
Chris@0: my ($self, $parms, $arg) = @_;
Chris@0: if ($arg) {
Chris@0: $self->{RedmineCachePool} = APR::Pool->new;
Chris@0: $self->{RedmineCacheCreds} = APR::Table::make($self->{RedmineCachePool}, $arg);
Chris@0: $self->{RedmineCacheCredsCount} = 0;
Chris@0: $self->{RedmineCacheCredsMax} = $arg;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: sub trim {
Chris@0: my $string = shift;
Chris@0: $string =~ s/\s{2,}/ /g;
Chris@0: return $string;
Chris@0: }
Chris@0:
Chris@0: sub set_val {
Chris@0: my ($key, $self, $parms, $arg) = @_;
Chris@0: $self->{$key} = $arg;
Chris@0: }
Chris@0:
Chris@0: Apache2::Module::add(__PACKAGE__, \@directives);
Chris@0:
Chris@0:
Chris@0: my %read_only_methods = map { $_ => 1 } qw/GET PROPFIND REPORT OPTIONS/;
Chris@0:
Chris@0: sub access_handler {
Chris@0: my $r = shift;
Chris@0:
Chris@0: unless ($r->some_auth_required) {
Chris@0: $r->log_reason("No authentication has been configured");
Chris@0: return FORBIDDEN;
Chris@0: }
Chris@0:
Chris@0: my $method = $r->method;
Chris@0: return OK unless defined $read_only_methods{$method};
Chris@0:
Chris@0: my $project_id = get_project_identifier($r);
Chris@0:
Chris@0: $r->set_handlers(PerlAuthenHandler => [\&OK])
Chris@0: if is_public_project($project_id, $r);
Chris@0:
Chris@0: return OK
Chris@0: }
Chris@0:
Chris@0: sub authen_handler {
Chris@0: my $r = shift;
Chris@0:
Chris@0: my ($res, $redmine_pass) = $r->get_basic_auth_pw();
Chris@0: return $res unless $res == OK;
Chris@0:
Chris@0: if (is_member($r->user, $redmine_pass, $r)) {
Chris@0: return OK;
Chris@0: } else {
Chris@0: $r->note_auth_failure();
Chris@0: return AUTH_REQUIRED;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: # check if authentication is forced
Chris@0: sub is_authentication_forced {
Chris@0: my $r = shift;
Chris@0:
Chris@0: my $dbh = connect_database($r);
Chris@0: my $sth = $dbh->prepare(
Chris@0: "SELECT value FROM settings where settings.name = 'login_required';"
Chris@0: );
Chris@0:
Chris@0: $sth->execute();
Chris@0: my $ret = 0;
Chris@0: if (my @row = $sth->fetchrow_array) {
Chris@0: if ($row[0] eq "1" || $row[0] eq "t") {
Chris@0: $ret = 1;
Chris@0: }
Chris@0: }
Chris@0: $sth->finish();
Chris@0: undef $sth;
Chris@0:
Chris@0: $dbh->disconnect();
Chris@0: undef $dbh;
Chris@0:
Chris@0: $ret;
Chris@0: }
Chris@0:
Chris@0: sub is_public_project {
Chris@0: my $project_id = shift;
Chris@0: my $r = shift;
Chris@0:
Chris@0: if (is_authentication_forced($r)) {
Chris@0: return 0;
Chris@0: }
Chris@0:
Chris@0: my $dbh = connect_database($r);
Chris@0: my $sth = $dbh->prepare(
Chris@0: "SELECT is_public FROM projects WHERE projects.identifier = ?;"
Chris@0: );
Chris@0:
Chris@0: $sth->execute($project_id);
Chris@0: my $ret = 0;
Chris@0: if (my @row = $sth->fetchrow_array) {
Chris@0: if ($row[0] eq "1" || $row[0] eq "t") {
Chris@0: $ret = 1;
Chris@0: }
Chris@0: }
Chris@0: $sth->finish();
Chris@0: undef $sth;
Chris@0: $dbh->disconnect();
Chris@0: undef $dbh;
Chris@0:
Chris@0: $ret;
Chris@0: }
Chris@0:
Chris@0: # perhaps we should use repository right (other read right) to check public access.
Chris@0: # it could be faster BUT it doesn't work for the moment.
Chris@0: # sub is_public_project_by_file {
Chris@0: # my $project_id = shift;
Chris@0: # my $r = shift;
Chris@0:
Chris@0: # my $tree = Apache2::Directive::conftree();
Chris@0: # my $node = $tree->lookup('Location', $r->location);
Chris@0: # my $hash = $node->as_hash;
Chris@0:
Chris@0: # my $svnparentpath = $hash->{SVNParentPath};
Chris@0: # my $repos_path = $svnparentpath . "/" . $project_id;
Chris@0: # return 1 if (stat($repos_path))[2] & 00007;
Chris@0: # }
Chris@0:
Chris@0: sub is_member {
Chris@0: my $redmine_user = shift;
Chris@0: my $redmine_pass = shift;
Chris@0: my $r = shift;
Chris@0:
Chris@0: my $dbh = connect_database($r);
Chris@0: my $project_id = get_project_identifier($r);
Chris@0:
Chris@0: my $pass_digest = Digest::SHA1::sha1_hex($redmine_pass);
Chris@0:
Chris@0: my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config);
Chris@0: my $usrprojpass;
Chris@0: if ($cfg->{RedmineCacheCredsMax}) {
Chris@0: $usrprojpass = $cfg->{RedmineCacheCreds}->get($redmine_user.":".$project_id);
Chris@0: return 1 if (defined $usrprojpass and ($usrprojpass eq $pass_digest));
Chris@0: }
Chris@0: my $query = $cfg->{RedmineQuery};
Chris@0: my $sth = $dbh->prepare($query);
Chris@0: $sth->execute($redmine_user, $project_id);
Chris@0:
Chris@0: my $ret;
Chris@245: while (my ($hashed_password, $salt, $auth_source_id, $permissions) = $sth->fetchrow_array) {
Chris@0:
Chris@0: unless ($auth_source_id) {
Chris@245: my $method = $r->method;
Chris@245: my $salted_password = Digest::SHA1::sha1_hex($salt.$pass_digest);
Chris@245: if ($hashed_password eq $salted_password && ((defined $read_only_methods{$method} && $permissions =~ /:browse_repository/) || $permissions =~ /:commit_access/) ) {
Chris@0: $ret = 1;
Chris@0: last;
Chris@0: }
Chris@0: } elsif ($CanUseLDAPAuth) {
Chris@0: my $sthldap = $dbh->prepare(
Chris@0: "SELECT host,port,tls,account,account_password,base_dn,attr_login from auth_sources WHERE id = ?;"
Chris@0: );
Chris@0: $sthldap->execute($auth_source_id);
Chris@0: while (my @rowldap = $sthldap->fetchrow_array) {
Chris@0: my $ldap = Authen::Simple::LDAP->new(
chris@37: host => ($rowldap[2] eq "1" || $rowldap[2] eq "t") ? "ldaps://$rowldap[0]:$rowldap[1]" : $rowldap[0],
Chris@0: port => $rowldap[1],
Chris@0: basedn => $rowldap[5],
Chris@0: binddn => $rowldap[3] ? $rowldap[3] : "",
Chris@0: bindpw => $rowldap[4] ? $rowldap[4] : "",
Chris@0: filter => "(".$rowldap[6]."=%s)"
Chris@0: );
Chris@0: my $method = $r->method;
Chris@0: $ret = 1 if ($ldap->authenticate($redmine_user, $redmine_pass) && ((defined $read_only_methods{$method} && $permissions =~ /:browse_repository/) || $permissions =~ /:commit_access/));
Chris@0:
Chris@0: }
Chris@0: $sthldap->finish();
Chris@0: undef $sthldap;
Chris@0: }
Chris@0: }
Chris@0: $sth->finish();
Chris@0: undef $sth;
Chris@0: $dbh->disconnect();
Chris@0: undef $dbh;
Chris@0:
Chris@0: if ($cfg->{RedmineCacheCredsMax} and $ret) {
Chris@0: if (defined $usrprojpass) {
Chris@0: $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id, $pass_digest);
Chris@0: } else {
Chris@0: if ($cfg->{RedmineCacheCredsCount} < $cfg->{RedmineCacheCredsMax}) {
Chris@0: $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id, $pass_digest);
Chris@0: $cfg->{RedmineCacheCredsCount}++;
Chris@0: } else {
Chris@0: $cfg->{RedmineCacheCreds}->clear();
Chris@0: $cfg->{RedmineCacheCredsCount} = 0;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: $ret;
Chris@0: }
Chris@0:
Chris@0: sub get_project_identifier {
Chris@0: my $r = shift;
Chris@0:
Chris@0: my $location = $r->location;
Chris@0: my ($identifier) = $r->uri =~ m{$location/*([^/]+)};
Chris@0: $identifier;
Chris@0: }
Chris@0:
Chris@0: sub connect_database {
Chris@0: my $r = shift;
Chris@0:
Chris@0: my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config);
Chris@0: return DBI->connect($cfg->{RedmineDSN}, $cfg->{RedmineDbUser}, $cfg->{RedmineDbPass});
Chris@0: }
Chris@0:
Chris@0: 1;