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