annotate extra/soundsoftware/SoundSoftware.pm @ 1474:c4436fec34bf bug_494

Close obsolete branch bug_494
author Chris Cannam
date Sat, 10 Nov 2012 13:57:53 +0000
parents 1ce6efe3db0e
children cf4cc816278a
rev   line source
Chris@7 1 package Apache::Authn::SoundSoftware;
Chris@7 2
Chris@7 3 =head1 Apache::Authn::SoundSoftware
Chris@7 4
Chris@7 5 SoundSoftware - a mod_perl module for Apache authentication against a
Chris@7 6 Redmine database and optional LDAP implementing the access control
Chris@7 7 rules required for the SoundSoftware.ac.uk repository site.
Chris@7 8
Chris@7 9 =head1 SYNOPSIS
Chris@7 10
Chris@7 11 This module is closely based on the Redmine.pm authentication module
Chris@7 12 provided with Redmine. It is intended to be used for authentication
Chris@7 13 in front of a repository service such as hgwebdir.
Chris@7 14
Chris@7 15 Requirements:
Chris@7 16
Chris@7 17 1. Clone/pull from repo for public project: Any user, no
Chris@7 18 authentication required
Chris@7 19
Chris@7 20 2. Clone/pull from repo for private project: Project members only
Chris@7 21
Chris@7 22 3. Push to repo for public project: "Permitted" users only (this
Chris@8 23 probably means project members who are also identified in the hgrc web
Chris@8 24 section for the repository and so will be approved by hgwebdir?)
Chris@7 25
Chris@8 26 4. Push to repo for private project: "Permitted" users only (as above)
Chris@7 27
chris@300 28 5. Push to any repo that is tracking an external repo: Refused always
chris@300 29
Chris@7 30 =head1 INSTALLATION
Chris@7 31
Chris@7 32 Debian/ubuntu:
Chris@7 33
Chris@7 34 apt-get install libapache-dbi-perl libapache2-mod-perl2 \
Chris@7 35 libdbd-mysql-perl libauthen-simple-ldap-perl libio-socket-ssl-perl
Chris@7 36
Chris@7 37 Note that LDAP support is hardcoded "on" in this script (it is
Chris@7 38 optional in the original Redmine.pm).
Chris@7 39
Chris@7 40 =head1 CONFIGURATION
Chris@7 41
Chris@7 42 ## This module has to be in your perl path
Chris@7 43 ## eg: /usr/local/lib/site_perl/Apache/Authn/SoundSoftware.pm
Chris@7 44 PerlLoadModule Apache::Authn::SoundSoftware
Chris@7 45
Chris@7 46 # Example when using hgwebdir
Chris@7 47 ScriptAlias / "/var/hg/hgwebdir.cgi/"
Chris@7 48
Chris@7 49 <Location />
Chris@7 50 AuthName "Mercurial"
Chris@7 51 AuthType Basic
Chris@7 52 Require valid-user
Chris@7 53 PerlAccessHandler Apache::Authn::SoundSoftware::access_handler
Chris@7 54 PerlAuthenHandler Apache::Authn::SoundSoftware::authen_handler
Chris@7 55 SoundSoftwareDSN "DBI:mysql:database=redmine;host=localhost"
Chris@7 56 SoundSoftwareDbUser "redmine"
Chris@7 57 SoundSoftwareDbPass "password"
Chris@7 58 Options +ExecCGI
Chris@7 59 AddHandler cgi-script .cgi
Chris@7 60 ## Optional where clause (fulltext search would be slow and
Chris@7 61 ## database dependant).
Chris@7 62 # SoundSoftwareDbWhereClause "and members.role_id IN (1,2)"
Chris@8 63 ## Optional prefix for local repository URLs
Chris@8 64 # SoundSoftwareRepoPrefix "/var/hg/"
Chris@7 65 </Location>
Chris@7 66
Chris@7 67 See the original Redmine.pm for further configuration notes.
Chris@7 68
Chris@7 69 =cut
Chris@7 70
Chris@7 71 use strict;
Chris@7 72 use warnings FATAL => 'all', NONFATAL => 'redefine';
Chris@7 73
Chris@7 74 use DBI;
Chris@7 75 use Digest::SHA1;
Chris@7 76 use Authen::Simple::LDAP;
Chris@7 77 use Apache2::Module;
Chris@7 78 use Apache2::Access;
Chris@7 79 use Apache2::ServerRec qw();
Chris@7 80 use Apache2::RequestRec qw();
Chris@7 81 use Apache2::RequestUtil qw();
Chris@7 82 use Apache2::Const qw(:common :override :cmd_how);
Chris@7 83 use APR::Pool ();
Chris@7 84 use APR::Table ();
Chris@7 85
Chris@7 86 my @directives = (
Chris@7 87 {
Chris@7 88 name => 'SoundSoftwareDSN',
Chris@7 89 req_override => OR_AUTHCFG,
Chris@7 90 args_how => TAKE1,
Chris@7 91 errmsg => 'Dsn in format used by Perl DBI. eg: "DBI:Pg:dbname=databasename;host=my.db.server"',
Chris@7 92 },
Chris@7 93 {
Chris@7 94 name => 'SoundSoftwareDbUser',
Chris@7 95 req_override => OR_AUTHCFG,
Chris@7 96 args_how => TAKE1,
Chris@7 97 },
Chris@7 98 {
Chris@7 99 name => 'SoundSoftwareDbPass',
Chris@7 100 req_override => OR_AUTHCFG,
Chris@7 101 args_how => TAKE1,
Chris@7 102 },
Chris@7 103 {
Chris@7 104 name => 'SoundSoftwareDbWhereClause',
Chris@7 105 req_override => OR_AUTHCFG,
Chris@7 106 args_how => TAKE1,
Chris@7 107 },
Chris@7 108 {
Chris@8 109 name => 'SoundSoftwareRepoPrefix',
Chris@7 110 req_override => OR_AUTHCFG,
Chris@7 111 args_how => TAKE1,
Chris@7 112 },
Chris@732 113 {
Chris@732 114 name => 'SoundSoftwareSslRequired',
Chris@732 115 req_override => OR_AUTHCFG,
Chris@732 116 args_how => TAKE1,
Chris@732 117 },
Chris@7 118 );
Chris@7 119
Chris@7 120 sub SoundSoftwareDSN {
Chris@8 121 my ($self, $parms, $arg) = @_;
Chris@8 122 $self->{SoundSoftwareDSN} = $arg;
Chris@8 123 my $query = "SELECT
chris@301 124 hashed_password, salt, auth_source_id, permissions
Chris@7 125 FROM members, projects, users, roles, member_roles
Chris@7 126 WHERE
Chris@7 127 projects.id=members.project_id
Chris@7 128 AND member_roles.member_id=members.id
Chris@7 129 AND users.id=members.user_id
Chris@7 130 AND roles.id=member_roles.role_id
Chris@7 131 AND users.status=1
Chris@7 132 AND login=?
Chris@7 133 AND identifier=? ";
Chris@8 134 $self->{SoundSoftwareQuery} = trim($query);
Chris@7 135 }
Chris@7 136
Chris@7 137 sub SoundSoftwareDbUser { set_val('SoundSoftwareDbUser', @_); }
Chris@7 138 sub SoundSoftwareDbPass { set_val('SoundSoftwareDbPass', @_); }
Chris@7 139 sub SoundSoftwareDbWhereClause {
Chris@8 140 my ($self, $parms, $arg) = @_;
Chris@8 141 $self->{SoundSoftwareQuery} = trim($self->{SoundSoftwareQuery}.($arg ? $arg : "")." ");
Chris@7 142 }
Chris@7 143
Chris@8 144 sub SoundSoftwareRepoPrefix {
Chris@8 145 my ($self, $parms, $arg) = @_;
Chris@8 146 if ($arg) {
Chris@8 147 $self->{SoundSoftwareRepoPrefix} = $arg;
Chris@8 148 }
Chris@7 149 }
Chris@7 150
Chris@732 151 sub SoundSoftwareSslRequired { set_val('SoundSoftwareSslRequired', @_); }
Chris@732 152
Chris@7 153 sub trim {
Chris@8 154 my $string = shift;
Chris@8 155 $string =~ s/\s{2,}/ /g;
Chris@8 156 return $string;
Chris@7 157 }
Chris@7 158
Chris@7 159 sub set_val {
Chris@8 160 my ($key, $self, $parms, $arg) = @_;
Chris@8 161 $self->{$key} = $arg;
Chris@7 162 }
Chris@7 163
Chris@7 164 Apache2::Module::add(__PACKAGE__, \@directives);
Chris@7 165
Chris@7 166
Chris@7 167 my %read_only_methods = map { $_ => 1 } qw/GET PROPFIND REPORT OPTIONS/;
Chris@7 168
Chris@7 169 sub access_handler {
Chris@8 170 my $r = shift;
Chris@7 171
Chris@517 172 print STDERR "SoundSoftware.pm:$$: In access handler at " . scalar localtime() . "\n";
Chris@7 173
Chris@8 174 unless ($r->some_auth_required) {
Chris@8 175 $r->log_reason("No authentication has been configured");
Chris@8 176 return FORBIDDEN;
Chris@8 177 }
Chris@7 178
Chris@8 179 my $method = $r->method;
Chris@7 180
Chris@517 181 print STDERR "SoundSoftware.pm:$$: Method: $method, uri " . $r->uri . ", location " . $r->location . "\n";
Chris@517 182 print STDERR "SoundSoftware.pm:$$: Accept: " . $r->headers_in->{Accept} . "\n";
Chris@7 183
Chris@8 184 my $dbh = connect_database($r);
Chris@152 185 unless ($dbh) {
Chris@517 186 print STDERR "SoundSoftware.pm:$$: Database connection failed!: " . $DBI::errstr . "\n";
Chris@152 187 return FORBIDDEN;
Chris@152 188 }
Chris@152 189
chris@300 190 print STDERR "Connected to db, dbh is " . $dbh . "\n";
Chris@7 191
Chris@8 192 my $project_id = get_project_identifier($dbh, $r);
chris@300 193
Chris@732 194 # We want to delegate most of the work to the authentication
Chris@732 195 # handler (to ensure that user is asked to login even for
Chris@732 196 # nonexistent projects -- so they can't tell whether a private
Chris@732 197 # project exists or not without authenticating). So
Chris@732 198 #
Chris@732 199 # * if the project is public
Chris@732 200 # - if the method is read-only
Chris@732 201 # + set handler to OK, no auth needed
Chris@732 202 # - if the method is not read-only
Chris@732 203 # + if the repo is read-only, return forbidden
Chris@732 204 # + else require auth
Chris@732 205 # * if the project is not public or does not exist
Chris@732 206 # + require auth
Chris@732 207 #
Chris@732 208 # If we are requiring auth and are not currently https, and
Chris@732 209 # https is required, then we must return a redirect to https
Chris@732 210 # instead of an OK.
chris@300 211
Chris@8 212 my $status = get_project_status($dbh, $project_id, $r);
Chris@732 213 my $readonly = project_repo_is_readonly($dbh, $project_id, $r);
Chris@7 214
Chris@8 215 $dbh->disconnect();
Chris@8 216 undef $dbh;
Chris@7 217
Chris@734 218 my $auth_ssl_reqd = will_require_ssl_auth($r);
Chris@734 219
Chris@732 220 if ($status == 1) { # public
Chris@732 221
Chris@732 222 print STDERR "SoundSoftware.pm:$$: Project is public\n";
Chris@732 223
Chris@732 224 if (!defined $read_only_methods{$method}) {
Chris@732 225
Chris@732 226 print STDERR "SoundSoftware.pm:$$: Method is not read-only\n";
Chris@732 227
Chris@732 228 if ($readonly) {
Chris@732 229 print STDERR "SoundSoftware.pm:$$: Project repo is read-only, refusing access\n";
Chris@732 230 return FORBIDDEN;
Chris@732 231 } else {
Chris@732 232 print STDERR "SoundSoftware.pm:$$: Project repo is read-write, auth required\n";
Chris@732 233 # fall through, this is the normal case
Chris@732 234 }
Chris@732 235
Chris@734 236 } elsif ($auth_ssl_reqd and $r->unparsed_uri =~ m/cmd=branchmap/) {
Chris@734 237
Chris@734 238 # A hac^H^H^Hspecial case. We want to ensure we switch to
Chris@734 239 # https (if it will be necessarily for authentication)
Chris@734 240 # before the first POST request, and this is what I think
Chris@734 241 # will give us suitable warning for Mercurial.
Chris@734 242
Chris@734 243 print STDERR "SoundSoftware.pm:$$: Switching to HTTPS in preparation\n";
Chris@734 244 # fall through, this is the normal case
Chris@734 245
Chris@732 246 } else {
Chris@732 247 # Public project, read-only method -- this is the only
Chris@732 248 # case we can decide for certain to accept in this function
Chris@732 249 print STDERR "SoundSoftware.pm:$$: Method is read-only, no restriction here\n";
Chris@732 250 $r->set_handlers(PerlAuthenHandler => [\&OK]);
Chris@732 251 return OK;
Chris@732 252 }
Chris@732 253
Chris@732 254 } else { # status != 1, i.e. nonexistent or private -- equivalent here
Chris@732 255
Chris@732 256 print STDERR "SoundSoftware.pm:$$: Project is private or nonexistent, auth required\n";
Chris@732 257 # fall through
Chris@8 258 }
Chris@7 259
Chris@734 260 if ($auth_ssl_reqd) {
Chris@734 261 my $redir_to = "https://" . $r->hostname() . $r->unparsed_uri();
Chris@734 262 print STDERR "SoundSoftware.pm:$$: Need to switch to HTTPS, redirecting to $redir_to\n";
Chris@734 263 $r->headers_out->add('Location' => $redir_to);
Chris@734 264 return REDIRECT;
Chris@732 265 } else {
Chris@734 266 return OK;
Chris@732 267 }
Chris@7 268 }
Chris@7 269
Chris@7 270 sub authen_handler {
Chris@8 271 my $r = shift;
Chris@8 272
Chris@517 273 print STDERR "SoundSoftware.pm:$$: In authentication handler at " . scalar localtime() . "\n";
Chris@7 274
Chris@8 275 my $dbh = connect_database($r);
Chris@152 276 unless ($dbh) {
Chris@517 277 print STDERR "SoundSoftware.pm:$$: Database connection failed!: " . $DBI::errstr . "\n";
Chris@152 278 return AUTH_REQUIRED;
Chris@152 279 }
Chris@8 280
Chris@8 281 my $project_id = get_project_identifier($dbh, $r);
Chris@8 282 my $realm = get_realm($dbh, $project_id, $r);
Chris@8 283 $r->auth_name($realm);
Chris@8 284
Chris@8 285 my ($res, $redmine_pass) = $r->get_basic_auth_pw();
Chris@8 286 unless ($res == OK) {
Chris@8 287 $dbh->disconnect();
Chris@8 288 undef $dbh;
Chris@8 289 return $res;
Chris@8 290 }
Chris@8 291
Chris@517 292 print STDERR "SoundSoftware.pm:$$: User is " . $r->user . ", got password\n";
Chris@8 293
Chris@732 294 my $status = get_project_status($dbh, $project_id, $r);
Chris@732 295 if ($status == 0) {
Chris@732 296 # nonexistent, behave like private project you aren't a member of
Chris@732 297 print STDERR "SoundSoftware.pm:$$: Project doesn't exist, not permitted\n";
Chris@732 298 $dbh->disconnect();
Chris@732 299 undef $dbh;
Chris@732 300 $r->note_auth_failure();
Chris@732 301 return AUTH_REQUIRED;
Chris@732 302 }
Chris@732 303
Chris@8 304 my $permitted = is_permitted($dbh, $project_id, $r->user, $redmine_pass, $r);
Chris@8 305
Chris@8 306 $dbh->disconnect();
Chris@8 307 undef $dbh;
Chris@8 308
Chris@8 309 if ($permitted) {
Chris@8 310 return OK;
Chris@8 311 } else {
Chris@517 312 print STDERR "SoundSoftware.pm:$$: Not permitted\n";
Chris@8 313 $r->note_auth_failure();
Chris@8 314 return AUTH_REQUIRED;
Chris@8 315 }
Chris@7 316 }
Chris@7 317
Chris@7 318 sub get_project_status {
Chris@8 319 my $dbh = shift;
Chris@7 320 my $project_id = shift;
Chris@7 321 my $r = shift;
Chris@8 322
Chris@8 323 if (!defined $project_id or $project_id eq '') {
Chris@8 324 return 0; # nonexistent
Chris@8 325 }
Chris@7 326
Chris@7 327 my $sth = $dbh->prepare(
Chris@7 328 "SELECT is_public FROM projects WHERE projects.identifier = ?;"
Chris@7 329 );
Chris@7 330
Chris@7 331 $sth->execute($project_id);
Chris@8 332 my $ret = 0; # nonexistent
Chris@7 333 if (my @row = $sth->fetchrow_array) {
Chris@7 334 if ($row[0] eq "1" || $row[0] eq "t") {
Chris@7 335 $ret = 1; # public
Chris@7 336 } else {
Chris@8 337 $ret = 2; # private
Chris@7 338 }
Chris@7 339 }
Chris@7 340 $sth->finish();
Chris@7 341 undef $sth;
Chris@7 342
Chris@7 343 $ret;
Chris@7 344 }
Chris@7 345
Chris@734 346 sub will_require_ssl_auth {
Chris@734 347 my $r = shift;
Chris@734 348
Chris@734 349 my $cfg = Apache2::Module::get_config
Chris@734 350 (__PACKAGE__, $r->server, $r->per_dir_config);
Chris@734 351
Chris@734 352 if ($cfg->{SoundSoftwareSslRequired} eq "on") {
Chris@734 353 if ($r->dir_config('HTTPS') eq "on") {
Chris@734 354 # already have ssl
Chris@734 355 return 0;
Chris@734 356 } else {
Chris@734 357 # require ssl for auth, don't have it yet
Chris@734 358 return 1;
Chris@734 359 }
Chris@734 360 } elsif ($cfg->{SoundSoftwareSslRequired} eq "off") {
Chris@734 361 # don't require ssl for auth
Chris@734 362 return 0;
Chris@734 363 } else {
Chris@734 364 print STDERR "WARNING: SoundSoftware.pm:$$: SoundSoftwareSslRequired should be either 'on' or 'off'\n";
Chris@734 365 # this is safer
Chris@734 366 return 1;
Chris@734 367 }
Chris@734 368 }
Chris@734 369
chris@300 370 sub project_repo_is_readonly {
chris@300 371 my $dbh = shift;
chris@300 372 my $project_id = shift;
chris@300 373 my $r = shift;
chris@300 374
chris@300 375 if (!defined $project_id or $project_id eq '') {
chris@300 376 return 0; # nonexistent
chris@300 377 }
chris@300 378
chris@300 379 my $sth = $dbh->prepare(
chris@300 380 "SELECT repositories.is_external FROM repositories, projects WHERE projects.identifier = ? AND repositories.project_id = projects.id;"
chris@300 381 );
chris@300 382
chris@300 383 $sth->execute($project_id);
chris@300 384 my $ret = 0; # nonexistent
chris@300 385 if (my @row = $sth->fetchrow_array) {
chris@301 386 if (defined($row[0]) && ($row[0] eq "1" || $row[0] eq "t")) {
chris@300 387 $ret = 1; # read-only (i.e. external)
chris@300 388 } else {
chris@300 389 $ret = 0; # read-write
chris@300 390 }
chris@300 391 }
chris@300 392 $sth->finish();
chris@300 393 undef $sth;
chris@300 394
chris@300 395 $ret;
chris@300 396 }
chris@300 397
Chris@8 398 sub is_permitted {
Chris@8 399 my $dbh = shift;
Chris@8 400 my $project_id = shift;
Chris@8 401 my $redmine_user = shift;
Chris@8 402 my $redmine_pass = shift;
Chris@8 403 my $r = shift;
Chris@7 404
Chris@8 405 my $pass_digest = Digest::SHA1::sha1_hex($redmine_pass);
Chris@7 406
Chris@8 407 my $cfg = Apache2::Module::get_config
Chris@8 408 (__PACKAGE__, $r->server, $r->per_dir_config);
Chris@7 409
Chris@8 410 my $query = $cfg->{SoundSoftwareQuery};
Chris@8 411 my $sth = $dbh->prepare($query);
Chris@8 412 $sth->execute($redmine_user, $project_id);
Chris@7 413
Chris@8 414 my $ret;
chris@301 415 while (my ($hashed_password, $salt, $auth_source_id, $permissions) = $sth->fetchrow_array) {
Chris@7 416
Chris@8 417 # Test permissions for this user before we verify credentials
Chris@8 418 # -- if the user is not permitted this action anyway, there's
Chris@8 419 # not much point in e.g. contacting the LDAP
Chris@7 420
Chris@8 421 my $method = $r->method;
Chris@7 422
Chris@8 423 if ((defined $read_only_methods{$method} && $permissions =~ /:browse_repository/)
Chris@8 424 || $permissions =~ /:commit_access/) {
Chris@8 425
Chris@8 426 # User would be permitted this action, if their
Chris@8 427 # credentials checked out -- test those now
Chris@8 428
Chris@8 429 print STDERR "SoundSoftware.pm: User $redmine_user has required role, checking credentials\n";
Chris@8 430
Chris@8 431 unless ($auth_source_id) {
chris@301 432 my $salted_password = Digest::SHA1::sha1_hex($salt.$pass_digest);
chris@301 433 if ($hashed_password eq $salted_password) {
Chris@8 434 print STDERR "SoundSoftware.pm: User $redmine_user authenticated via password\n";
Chris@8 435 $ret = 1;
Chris@8 436 last;
Chris@8 437 }
Chris@8 438 } else {
Chris@8 439 my $sthldap = $dbh->prepare(
Chris@8 440 "SELECT host,port,tls,account,account_password,base_dn,attr_login FROM auth_sources WHERE id = ?;"
Chris@8 441 );
Chris@8 442 $sthldap->execute($auth_source_id);
Chris@8 443 while (my @rowldap = $sthldap->fetchrow_array) {
Chris@8 444 my $ldap = Authen::Simple::LDAP->new(
Chris@8 445 host => ($rowldap[2] eq "1" || $rowldap[2] eq "t") ? "ldaps://$rowldap[0]" : $rowldap[0],
Chris@8 446 port => $rowldap[1],
Chris@8 447 basedn => $rowldap[5],
Chris@8 448 binddn => $rowldap[3] ? $rowldap[3] : "",
Chris@8 449 bindpw => $rowldap[4] ? $rowldap[4] : "",
Chris@8 450 filter => "(".$rowldap[6]."=%s)"
Chris@8 451 );
Chris@8 452 if ($ldap->authenticate($redmine_user, $redmine_pass)) {
Chris@517 453 print STDERR "SoundSoftware.pm:$$: User $redmine_user authenticated via LDAP\n";
Chris@8 454 $ret = 1;
Chris@8 455 }
Chris@8 456 }
Chris@8 457 $sthldap->finish();
Chris@8 458 undef $sthldap;
Chris@735 459 last if ($ret);
Chris@8 460 }
Chris@8 461 } else {
Chris@517 462 print STDERR "SoundSoftware.pm:$$: User $redmine_user lacks required role for this project\n";
Chris@8 463 }
Chris@7 464 }
Chris@7 465
Chris@8 466 $sth->finish();
Chris@8 467 undef $sth;
Chris@8 468
Chris@8 469 $ret;
Chris@7 470 }
Chris@7 471
Chris@7 472 sub get_project_identifier {
Chris@8 473 my $dbh = shift;
Chris@7 474 my $r = shift;
Chris@7 475 my $location = $r->location;
Chris@737 476 my ($repo) = $r->uri =~ m{$location/*([^/]*)};
Chris@10 477
Chris@10 478 return $repo if (!$repo);
Chris@10 479
Chris@7 480 $repo =~ s/[^a-zA-Z0-9\._-]//g;
Chris@736 481
Chris@8 482 # The original Redmine.pm returns the string just calculated as
Chris@8 483 # the project identifier. That won't do for us -- we may have
Chris@8 484 # (and in fact already do have, in our test instance) projects
Chris@8 485 # whose repository names differ from the project identifiers.
Chris@8 486
Chris@8 487 # This is a rather fundamental change because it means that almost
Chris@8 488 # every request needs more than one database query -- which
Chris@8 489 # prompts us to start passing around $dbh instead of connecting
Chris@8 490 # locally within each function as is done in Redmine.pm.
Chris@8 491
Chris@7 492 my $sth = $dbh->prepare(
Chris@7 493 "SELECT projects.identifier FROM projects, repositories WHERE repositories.project_id = projects.id AND repositories.url LIKE ?;"
Chris@7 494 );
Chris@7 495
Chris@8 496 my $cfg = Apache2::Module::get_config
Chris@8 497 (__PACKAGE__, $r->server, $r->per_dir_config);
Chris@8 498
Chris@8 499 my $prefix = $cfg->{SoundSoftwareRepoPrefix};
Chris@8 500 if (!defined $prefix) { $prefix = '%/'; }
Chris@7 501 my $identifier = '';
Chris@7 502
Chris@8 503 $sth->execute($prefix . $repo);
Chris@7 504 my $ret = 0;
Chris@7 505 if (my @row = $sth->fetchrow_array) {
Chris@7 506 $identifier = $row[0];
Chris@7 507 }
Chris@7 508 $sth->finish();
Chris@7 509 undef $sth;
Chris@7 510
Chris@517 511 print STDERR "SoundSoftware.pm:$$: Repository '$repo' belongs to project '$identifier'\n";
Chris@7 512
Chris@7 513 $identifier;
Chris@7 514 }
Chris@7 515
Chris@8 516 sub get_realm {
Chris@8 517 my $dbh = shift;
Chris@8 518 my $project_id = shift;
Chris@8 519 my $r = shift;
Chris@8 520
Chris@8 521 my $sth = $dbh->prepare(
Chris@8 522 "SELECT projects.name FROM projects WHERE projects.identifier = ?;"
Chris@8 523 );
Chris@8 524
Chris@8 525 my $name = $project_id;
Chris@8 526
Chris@8 527 $sth->execute($project_id);
Chris@8 528 my $ret = 0;
Chris@8 529 if (my @row = $sth->fetchrow_array) {
Chris@8 530 $name = $row[0];
Chris@8 531 }
Chris@8 532 $sth->finish();
Chris@8 533 undef $sth;
Chris@8 534
Chris@8 535 # be timid about characters not permitted in auth realm and revert
Chris@8 536 # to project identifier if any are found
Chris@8 537 if ($name =~ m/[^\w\d\s\._-]/) {
Chris@8 538 $name = $project_id;
Chris@733 539 } elsif ($name =~ m/^\s*$/) {
Chris@733 540 # empty or whitespace
Chris@733 541 $name = $project_id;
Chris@733 542 }
Chris@733 543
Chris@733 544 if ($name =~ m/^\s*$/) {
Chris@733 545 # nothing even in $project_id -- probably a nonexistent project.
Chris@733 546 # use repo name instead (don't want to admit to user that project
Chris@733 547 # doesn't exist)
Chris@733 548 my $location = $r->location;
Chris@737 549 my ($repo) = $r->uri =~ m{$location/*([^/]*)};
Chris@733 550 $name = $repo;
Chris@8 551 }
Chris@8 552
Chris@8 553 my $realm = '"Mercurial repository for ' . "'$name'" . '"';
Chris@8 554
Chris@8 555 $realm;
Chris@8 556 }
Chris@8 557
Chris@7 558 sub connect_database {
Chris@7 559 my $r = shift;
Chris@7 560
Chris@8 561 my $cfg = Apache2::Module::get_config
Chris@8 562 (__PACKAGE__, $r->server, $r->per_dir_config);
Chris@8 563
Chris@8 564 return DBI->connect($cfg->{SoundSoftwareDSN},
Chris@152 565 $cfg->{SoundSoftwareDbUser},
Chris@152 566 $cfg->{SoundSoftwareDbPass});
Chris@7 567 }
Chris@7 568
Chris@7 569 1;