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@909: 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@1115: =head1 REPOSITORIES NAMING Chris@1115: Chris@1115: A projet repository must be named with the projet identifier. In case Chris@1115: of multiple repositories for the same project, use the project identifier Chris@1115: and the repository identifier separated with a dot: Chris@1115: Chris@1115: /var/svn/foo Chris@1115: /var/svn/foo.otherrepo Chris@1115: 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@1115: =head1 GIT SMART HTTP SUPPORT Chris@1115: Chris@1115: Git's smart HTTP protocol (available since Git 1.7.0) will not work with the Chris@1115: above settings. Redmine.pm normally does access control depending on the HTTP Chris@1115: method used: read-only methods are OK for everyone in public projects and Chris@1115: members with read rights in private projects. The rest require membership with Chris@1115: commit rights in the project. Chris@1115: Chris@1115: However, this scheme doesn't work for Git's smart HTTP protocol, as it will use Chris@1115: POST even for a simple clone. Instead, read-only requests must be detected using Chris@1115: the full URL (including the query string): anything that doesn't belong to the Chris@1115: git-receive-pack service is read-only. Chris@1115: Chris@1115: To activate this mode of operation, add this line inside your Chris@1115: block: Chris@1115: Chris@1115: RedmineGitSmartHttp yes Chris@1115: Chris@1115: Here's a sample Apache configuration which integrates git-http-backend with Chris@1115: a MySQL database and this new option: Chris@1115: Chris@1115: SetEnv GIT_PROJECT_ROOT /var/www/git/ Chris@1115: SetEnv GIT_HTTP_EXPORT_ALL Chris@1115: ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/ Chris@1115: Chris@1115: Order allow,deny Chris@1115: Allow from all Chris@1115: Chris@1115: AuthType Basic Chris@1115: AuthName Git Chris@1115: Require valid-user Chris@1115: Chris@1115: PerlAccessHandler Apache::Authn::Redmine::access_handler Chris@1115: PerlAuthenHandler Apache::Authn::Redmine::authen_handler Chris@1115: # for mysql Chris@1115: RedmineDSN "DBI:mysql:database=redmine;host=127.0.0.1" Chris@1115: RedmineDbUser "redmine" Chris@1115: RedmineDbPass "xxx" Chris@1115: RedmineGitSmartHttp yes Chris@1115: Chris@1115: Chris@1115: Make sure that all the names of the repositories under /var/www/git/ have a Chris@1115: matching identifier for some project: /var/www/git/myproject and Chris@1115: /var/www/git/myproject.git will work. You can put both bare and non-bare Chris@1115: repositories in /var/www/git, though bare repositories are strongly Chris@1115: recommended. You should create them with the rights of the user running Redmine, Chris@1115: like this: Chris@1115: Chris@1115: cd /var/www/git Chris@1115: sudo -u user-running-redmine mkdir myproject Chris@1115: cd myproject Chris@1115: sudo -u user-running-redmine git init --bare Chris@1115: Chris@1115: Once you have activated this option, you have three options when cloning a Chris@1115: repository: Chris@1115: Chris@1115: - Cloning using "http://user@host/git/repo(.git)" works, but will ask for the password Chris@1115: all the time. Chris@1115: Chris@1115: - Cloning with "http://user:pass@host/git/repo(.git)" does not have this problem, but Chris@1115: this could reveal accidentally your password to the console in some versions Chris@1115: of Git, and you would have to ensure that .git/config is not readable except Chris@1115: by the owner for each of your projects. Chris@1115: Chris@1115: - Use "http://host/git/repo(.git)", and store your credentials in the ~/.netrc Chris@1115: file. This is the recommended solution, as you only have one file to protect Chris@1115: and passwords will not be leaked accidentally to the console. Chris@1115: Chris@1115: IMPORTANT NOTE: It is *very important* that the file cannot be read by other Chris@1115: users, as it will contain your password in cleartext. To create the file, you Chris@1115: can use the following commands, replacing yourhost, youruser and yourpassword Chris@1115: with the right values: Chris@1115: Chris@1115: touch ~/.netrc Chris@1115: chmod 600 ~/.netrc Chris@1115: echo -e "machine yourhost\nlogin youruser\npassword yourpassword" > ~/.netrc Chris@1115: Chris@0: =cut Chris@0: Chris@0: use strict; Chris@0: use warnings FATAL => 'all', NONFATAL => 'redefine'; Chris@0: Chris@0: use DBI; Chris@929: use Digest::SHA; 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@1115: { Chris@1115: name => 'RedmineGitSmartHttp', Chris@1115: req_override => OR_AUTHCFG, Chris@1115: args_how => TAKE1, Chris@1115: }, Chris@0: ); Chris@0: Chris@909: sub RedmineDSN { Chris@0: my ($self, $parms, $arg) = @_; Chris@0: $self->{RedmineDSN} = $arg; Chris@0: my $query = "SELECT Chris@1115: users.hashed_password, users.salt, users.auth_source_id, roles.permissions, projects.status Chris@909: FROM projects, users, roles Chris@0: WHERE Chris@909: users.login=? Chris@909: AND projects.identifier=? Chris@0: AND users.status=1 Chris@909: AND ( Chris@909: roles.id IN (SELECT member_roles.role_id FROM members, member_roles WHERE members.user_id = users.id AND members.project_id = projects.id AND members.id = member_roles.member_id) Chris@909: OR Chris@909: (roles.builtin=1 AND cast(projects.is_public as CHAR) IN ('t', '1')) Chris@1115: ) Chris@1115: AND roles.permissions IS NOT NULL"; 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@909: sub RedmineDbWhereClause { Chris@0: my ($self, $parms, $arg) = @_; Chris@0: $self->{RedmineQuery} = trim($self->{RedmineQuery}.($arg ? $arg : "")." "); Chris@0: } Chris@0: Chris@909: 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@1115: sub RedmineGitSmartHttp { Chris@1115: my ($self, $parms, $arg) = @_; Chris@1115: $arg = lc $arg; Chris@1115: Chris@1115: if ($arg eq "yes" || $arg eq "true") { Chris@1115: $self->{RedmineGitSmartHttp} = 1; Chris@1115: } else { Chris@1115: $self->{RedmineGitSmartHttp} = 0; Chris@1115: } Chris@1115: } Chris@1115: 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@1115: my %read_only_methods = map { $_ => 1 } qw/GET HEAD PROPFIND REPORT OPTIONS/; Chris@1115: Chris@1115: sub request_is_read_only { Chris@1115: my ($r) = @_; Chris@1115: my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config); Chris@1115: Chris@1115: # Do we use Git's smart HTTP protocol, or not? Chris@1115: if (defined $cfg->{RedmineGitSmartHttp} and $cfg->{RedmineGitSmartHttp}) { Chris@1115: my $uri = $r->unparsed_uri; Chris@1115: my $location = $r->location; Chris@1115: my $is_read_only = $uri !~ m{^$location/*[^/]+/+(info/refs\?service=)?git\-receive\-pack$}o; Chris@1115: return $is_read_only; Chris@1115: } else { Chris@1115: # Standard behaviour: check the HTTP method Chris@1115: my $method = $r->method; Chris@1115: return defined $read_only_methods{$method}; Chris@1115: } Chris@1115: } 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@1115: return OK unless request_is_read_only($r); Chris@0: Chris@0: my $project_id = get_project_identifier($r); Chris@0: Chris@0: $r->set_handlers(PerlAuthenHandler => [\&OK]) Chris@909: if is_public_project($project_id, $r) && anonymous_role_allows_browse_repository($r); Chris@0: Chris@0: return OK Chris@0: } Chris@0: Chris@0: sub authen_handler { Chris@0: my $r = shift; Chris@909: Chris@0: my ($res, $redmine_pass) = $r->get_basic_auth_pw(); Chris@0: return $res unless $res == OK; Chris@909: Chris@0: if (is_member($r->user, $redmine_pass, $r)) { Chris@0: return OK; Chris@0: } else { Chris@0: $r->note_auth_failure(); Chris@1115: return DECLINED; 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@909: 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@909: 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@1115: "SELECT is_public FROM projects WHERE projects.identifier = ? AND projects.status <> 9;" Chris@0: ); Chris@0: Chris@0: $sth->execute($project_id); Chris@0: my $ret = 0; Chris@0: if (my @row = $sth->fetchrow_array) { Chris@1464: if ($row[0] eq "1" || $row[0] eq "t") { Chris@1464: $ret = 1; Chris@1464: } 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@909: sub anonymous_role_allows_browse_repository { Chris@909: my $r = shift; Chris@909: Chris@909: my $dbh = connect_database($r); Chris@909: my $sth = $dbh->prepare( Chris@909: "SELECT permissions FROM roles WHERE builtin = 2;" Chris@909: ); Chris@909: Chris@909: $sth->execute(); Chris@909: my $ret = 0; Chris@909: if (my @row = $sth->fetchrow_array) { Chris@909: if ($row[0] =~ /:browse_repository/) { Chris@909: $ret = 1; Chris@909: } Chris@909: } Chris@909: $sth->finish(); Chris@909: undef $sth; Chris@909: $dbh->disconnect(); Chris@909: undef $dbh; Chris@909: Chris@909: $ret; Chris@909: } Chris@909: 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@929: my $pass_digest = Digest::SHA::sha1_hex($redmine_pass); Chris@0: Chris@1115: my $access_mode = request_is_read_only($r) ? "R" : "W"; Chris@909: 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@909: $usrprojpass = $cfg->{RedmineCacheCreds}->get($redmine_user.":".$project_id.":".$access_mode); 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@1115: while (my ($hashed_password, $salt, $auth_source_id, $permissions, $project_status) = $sth->fetchrow_array) { Chris@1115: if ($project_status eq "9" || ($project_status ne "1" && $access_mode eq "W")) { Chris@1115: last; Chris@1115: } Chris@0: Chris@0: unless ($auth_source_id) { Chris@1464: my $method = $r->method; Chris@929: my $salted_password = Digest::SHA::sha1_hex($salt.$pass_digest); Chris@1464: if ($hashed_password eq $salted_password && (($access_mode eq "R" && $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@1115: my $bind_as = $rowldap[3] ? $rowldap[3] : ""; Chris@1115: my $bind_pw = $rowldap[4] ? $rowldap[4] : ""; Chris@1115: if ($bind_as =~ m/\$login/) { Chris@1115: # replace $login with $redmine_user and use $redmine_pass Chris@1115: $bind_as =~ s/\$login/$redmine_user/g; Chris@1115: $bind_pw = $redmine_pass Chris@1115: } 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@1115: binddn => $bind_as, Chris@1115: bindpw => $bind_pw, Chris@0: filter => "(".$rowldap[6]."=%s)" Chris@0: ); Chris@0: my $method = $r->method; Chris@909: $ret = 1 if ($ldap->authenticate($redmine_user, $redmine_pass) && (($access_mode eq "R" && $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@909: $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id.":".$access_mode, $pass_digest); Chris@0: } else { Chris@0: if ($cfg->{RedmineCacheCredsCount} < $cfg->{RedmineCacheCredsMax}) { Chris@909: $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id.":".$access_mode, $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@909: Chris@1115: my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config); Chris@0: my $location = $r->location; Chris@1115: $location =~ s/\.git$// if (defined $cfg->{RedmineGitSmartHttp} and $cfg->{RedmineGitSmartHttp}); Chris@1115: my ($identifier) = $r->uri =~ m{$location/*([^/.]+)}; Chris@0: $identifier; Chris@0: } Chris@0: Chris@0: sub connect_database { Chris@0: my $r = shift; Chris@909: 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;