You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Redmine.pm 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package Apache::Authn::Redmine;
  2. =head1 Apache::Authn::Redmine
  3. Redmine - a mod_perl module to authenticate webdav subversion users
  4. against redmine database
  5. =head1 SYNOPSIS
  6. This module allow anonymous users to browse public project and
  7. registred users to browse and commit their project. Authentication is
  8. done against the redmine database or the LDAP configured in redmine.
  9. This method is far simpler than the one with pam_* and works with all
  10. database without an hassle but you need to have apache/mod_perl on the
  11. svn server.
  12. =head1 INSTALLATION
  13. For this to automagically work, you need to have a recent reposman.rb
  14. (after r860) and if you already use reposman, read the last section to
  15. migrate.
  16. Sorry ruby users but you need some perl modules, at least mod_perl2,
  17. DBI and DBD::mysql (or the DBD driver for you database as it should
  18. work on allmost all databases).
  19. On debian/ubuntu you must do :
  20. aptitude install libapache-dbi-perl libapache2-mod-perl2 libdbd-mysql-perl
  21. If your Redmine users use LDAP authentication, you will also need
  22. Authen::Simple::LDAP (and IO::Socket::SSL if LDAPS is used):
  23. aptitude install libauthen-simple-ldap-perl libio-socket-ssl-perl
  24. =head1 CONFIGURATION
  25. ## This module has to be in your perl path
  26. ## eg: /usr/lib/perl5/Apache/Authn/Redmine.pm
  27. PerlLoadModule Apache::Authn::Redmine
  28. <Location /svn>
  29. DAV svn
  30. SVNParentPath "/var/svn"
  31. AuthType Basic
  32. AuthName redmine
  33. Require valid-user
  34. PerlAccessHandler Apache::Authn::Redmine::access_handler
  35. PerlAuthenHandler Apache::Authn::Redmine::authen_handler
  36. ## for mysql
  37. RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
  38. ## for postgres
  39. # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
  40. RedmineDbUser "redmine"
  41. RedmineDbPass "password"
  42. ## Optional where clause (fulltext search would be slow and
  43. ## database dependant).
  44. # RedmineDbWhereClause "and members.role_id IN (1,2)"
  45. ## Optional credentials cache size
  46. # RedmineCacheCredsMax 50
  47. </Location>
  48. To be able to browse repository inside redmine, you must add something
  49. like that :
  50. <Location /svn-private>
  51. DAV svn
  52. SVNParentPath "/var/svn"
  53. Order deny,allow
  54. Deny from all
  55. # only allow reading orders
  56. <Limit GET PROPFIND OPTIONS REPORT>
  57. Allow from redmine.server.ip
  58. </Limit>
  59. </Location>
  60. and you will have to use this reposman.rb command line to create repository :
  61. reposman.rb --redmine my.redmine.server --svn-dir /var/svn --owner www-data -u http://svn.server/svn-private/
  62. =head1 MIGRATION FROM OLDER RELEASES
  63. If you use an older reposman.rb (r860 or before), you need to change
  64. rights on repositories to allow the apache user to read and write
  65. S<them :>
  66. sudo chown -R www-data /var/svn/*
  67. sudo chmod -R u+w /var/svn/*
  68. And you need to upgrade at least reposman.rb (after r860).
  69. =cut
  70. use strict;
  71. use warnings FATAL => 'all', NONFATAL => 'redefine';
  72. use DBI;
  73. use Digest::SHA1;
  74. # optional module for LDAP authentication
  75. my $CanUseLDAPAuth = eval("use Authen::Simple::LDAP; 1");
  76. use Apache2::Module;
  77. use Apache2::Access;
  78. use Apache2::ServerRec qw();
  79. use Apache2::RequestRec qw();
  80. use Apache2::RequestUtil qw();
  81. use Apache2::Const qw(:common :override :cmd_how);
  82. use APR::Pool ();
  83. use APR::Table ();
  84. # use Apache2::Directive qw();
  85. my @directives = (
  86. {
  87. name => 'RedmineDSN',
  88. req_override => OR_AUTHCFG,
  89. args_how => TAKE1,
  90. errmsg => 'Dsn in format used by Perl DBI. eg: "DBI:Pg:dbname=databasename;host=my.db.server"',
  91. },
  92. {
  93. name => 'RedmineDbUser',
  94. req_override => OR_AUTHCFG,
  95. args_how => TAKE1,
  96. },
  97. {
  98. name => 'RedmineDbPass',
  99. req_override => OR_AUTHCFG,
  100. args_how => TAKE1,
  101. },
  102. {
  103. name => 'RedmineDbWhereClause',
  104. req_override => OR_AUTHCFG,
  105. args_how => TAKE1,
  106. },
  107. {
  108. name => 'RedmineCacheCredsMax',
  109. req_override => OR_AUTHCFG,
  110. args_how => TAKE1,
  111. errmsg => 'RedmineCacheCredsMax must be decimal number',
  112. },
  113. );
  114. sub RedmineDSN {
  115. my ($self, $parms, $arg) = @_;
  116. $self->{RedmineDSN} = $arg;
  117. my $query = "SELECT
  118. hashed_password, salt, auth_source_id, permissions
  119. FROM projects, users, roles
  120. WHERE
  121. users.login=?
  122. AND projects.identifier=?
  123. AND users.status=1
  124. AND (
  125. 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)
  126. OR
  127. (roles.builtin=1 AND cast(projects.is_public as CHAR) IN ('t', '1'))
  128. ) ";
  129. $self->{RedmineQuery} = trim($query);
  130. }
  131. sub RedmineDbUser { set_val('RedmineDbUser', @_); }
  132. sub RedmineDbPass { set_val('RedmineDbPass', @_); }
  133. sub RedmineDbWhereClause {
  134. my ($self, $parms, $arg) = @_;
  135. $self->{RedmineQuery} = trim($self->{RedmineQuery}.($arg ? $arg : "")." ");
  136. }
  137. sub RedmineCacheCredsMax {
  138. my ($self, $parms, $arg) = @_;
  139. if ($arg) {
  140. $self->{RedmineCachePool} = APR::Pool->new;
  141. $self->{RedmineCacheCreds} = APR::Table::make($self->{RedmineCachePool}, $arg);
  142. $self->{RedmineCacheCredsCount} = 0;
  143. $self->{RedmineCacheCredsMax} = $arg;
  144. }
  145. }
  146. sub trim {
  147. my $string = shift;
  148. $string =~ s/\s{2,}/ /g;
  149. return $string;
  150. }
  151. sub set_val {
  152. my ($key, $self, $parms, $arg) = @_;
  153. $self->{$key} = $arg;
  154. }
  155. Apache2::Module::add(__PACKAGE__, \@directives);
  156. my %read_only_methods = map { $_ => 1 } qw/GET PROPFIND REPORT OPTIONS/;
  157. sub access_handler {
  158. my $r = shift;
  159. unless ($r->some_auth_required) {
  160. $r->log_reason("No authentication has been configured");
  161. return FORBIDDEN;
  162. }
  163. my $method = $r->method;
  164. return OK unless defined $read_only_methods{$method};
  165. my $project_id = get_project_identifier($r);
  166. $r->set_handlers(PerlAuthenHandler => [\&OK])
  167. if is_public_project($project_id, $r) && anonymous_role_allows_browse_repository($r);
  168. return OK
  169. }
  170. sub authen_handler {
  171. my $r = shift;
  172. my ($res, $redmine_pass) = $r->get_basic_auth_pw();
  173. return $res unless $res == OK;
  174. if (is_member($r->user, $redmine_pass, $r)) {
  175. return OK;
  176. } else {
  177. $r->note_auth_failure();
  178. return AUTH_REQUIRED;
  179. }
  180. }
  181. # check if authentication is forced
  182. sub is_authentication_forced {
  183. my $r = shift;
  184. my $dbh = connect_database($r);
  185. my $sth = $dbh->prepare(
  186. "SELECT value FROM settings where settings.name = 'login_required';"
  187. );
  188. $sth->execute();
  189. my $ret = 0;
  190. if (my @row = $sth->fetchrow_array) {
  191. if ($row[0] eq "1" || $row[0] eq "t") {
  192. $ret = 1;
  193. }
  194. }
  195. $sth->finish();
  196. undef $sth;
  197. $dbh->disconnect();
  198. undef $dbh;
  199. $ret;
  200. }
  201. sub is_public_project {
  202. my $project_id = shift;
  203. my $r = shift;
  204. if (is_authentication_forced($r)) {
  205. return 0;
  206. }
  207. my $dbh = connect_database($r);
  208. my $sth = $dbh->prepare(
  209. "SELECT is_public FROM projects WHERE projects.identifier = ?;"
  210. );
  211. $sth->execute($project_id);
  212. my $ret = 0;
  213. if (my @row = $sth->fetchrow_array) {
  214. if ($row[0] eq "1" || $row[0] eq "t") {
  215. $ret = 1;
  216. }
  217. }
  218. $sth->finish();
  219. undef $sth;
  220. $dbh->disconnect();
  221. undef $dbh;
  222. $ret;
  223. }
  224. sub anonymous_role_allows_browse_repository {
  225. my $r = shift;
  226. my $dbh = connect_database($r);
  227. my $sth = $dbh->prepare(
  228. "SELECT permissions FROM roles WHERE builtin = 2;"
  229. );
  230. $sth->execute();
  231. my $ret = 0;
  232. if (my @row = $sth->fetchrow_array) {
  233. if ($row[0] =~ /:browse_repository/) {
  234. $ret = 1;
  235. }
  236. }
  237. $sth->finish();
  238. undef $sth;
  239. $dbh->disconnect();
  240. undef $dbh;
  241. $ret;
  242. }
  243. # perhaps we should use repository right (other read right) to check public access.
  244. # it could be faster BUT it doesn't work for the moment.
  245. # sub is_public_project_by_file {
  246. # my $project_id = shift;
  247. # my $r = shift;
  248. # my $tree = Apache2::Directive::conftree();
  249. # my $node = $tree->lookup('Location', $r->location);
  250. # my $hash = $node->as_hash;
  251. # my $svnparentpath = $hash->{SVNParentPath};
  252. # my $repos_path = $svnparentpath . "/" . $project_id;
  253. # return 1 if (stat($repos_path))[2] & 00007;
  254. # }
  255. sub is_member {
  256. my $redmine_user = shift;
  257. my $redmine_pass = shift;
  258. my $r = shift;
  259. my $dbh = connect_database($r);
  260. my $project_id = get_project_identifier($r);
  261. my $pass_digest = Digest::SHA1::sha1_hex($redmine_pass);
  262. my $access_mode = defined $read_only_methods{$r->method} ? "R" : "W";
  263. my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config);
  264. my $usrprojpass;
  265. if ($cfg->{RedmineCacheCredsMax}) {
  266. $usrprojpass = $cfg->{RedmineCacheCreds}->get($redmine_user.":".$project_id.":".$access_mode);
  267. return 1 if (defined $usrprojpass and ($usrprojpass eq $pass_digest));
  268. }
  269. my $query = $cfg->{RedmineQuery};
  270. my $sth = $dbh->prepare($query);
  271. $sth->execute($redmine_user, $project_id);
  272. my $ret;
  273. while (my ($hashed_password, $salt, $auth_source_id, $permissions) = $sth->fetchrow_array) {
  274. unless ($auth_source_id) {
  275. my $method = $r->method;
  276. my $salted_password = Digest::SHA1::sha1_hex($salt.$pass_digest);
  277. if ($hashed_password eq $salted_password && (($access_mode eq "R" && $permissions =~ /:browse_repository/) || $permissions =~ /:commit_access/) ) {
  278. $ret = 1;
  279. last;
  280. }
  281. } elsif ($CanUseLDAPAuth) {
  282. my $sthldap = $dbh->prepare(
  283. "SELECT host,port,tls,account,account_password,base_dn,attr_login from auth_sources WHERE id = ?;"
  284. );
  285. $sthldap->execute($auth_source_id);
  286. while (my @rowldap = $sthldap->fetchrow_array) {
  287. my $ldap = Authen::Simple::LDAP->new(
  288. host => ($rowldap[2] eq "1" || $rowldap[2] eq "t") ? "ldaps://$rowldap[0]:$rowldap[1]" : $rowldap[0],
  289. port => $rowldap[1],
  290. basedn => $rowldap[5],
  291. binddn => $rowldap[3] ? $rowldap[3] : "",
  292. bindpw => $rowldap[4] ? $rowldap[4] : "",
  293. filter => "(".$rowldap[6]."=%s)"
  294. );
  295. my $method = $r->method;
  296. $ret = 1 if ($ldap->authenticate($redmine_user, $redmine_pass) && (($access_mode eq "R" && $permissions =~ /:browse_repository/) || $permissions =~ /:commit_access/));
  297. }
  298. $sthldap->finish();
  299. undef $sthldap;
  300. }
  301. }
  302. $sth->finish();
  303. undef $sth;
  304. $dbh->disconnect();
  305. undef $dbh;
  306. if ($cfg->{RedmineCacheCredsMax} and $ret) {
  307. if (defined $usrprojpass) {
  308. $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id.":".$access_mode, $pass_digest);
  309. } else {
  310. if ($cfg->{RedmineCacheCredsCount} < $cfg->{RedmineCacheCredsMax}) {
  311. $cfg->{RedmineCacheCreds}->set($redmine_user.":".$project_id.":".$access_mode, $pass_digest);
  312. $cfg->{RedmineCacheCredsCount}++;
  313. } else {
  314. $cfg->{RedmineCacheCreds}->clear();
  315. $cfg->{RedmineCacheCredsCount} = 0;
  316. }
  317. }
  318. }
  319. $ret;
  320. }
  321. sub get_project_identifier {
  322. my $r = shift;
  323. my $location = $r->location;
  324. my ($identifier) = $r->uri =~ m{$location/*([^/]+)};
  325. $identifier;
  326. }
  327. sub connect_database {
  328. my $r = shift;
  329. my $cfg = Apache2::Module::get_config(__PACKAGE__, $r->server, $r->per_dir_config);
  330. return DBI->connect($cfg->{RedmineDSN}, $cfg->{RedmineDbUser}, $cfg->{RedmineDbPass});
  331. }
  332. 1;