Changeset d5d5b4929dbf98f5a8953bcc43f6c24007be7b5f

Show
Ignore:
Timestamp:
02/18/10 20:16:12 (4 years ago)
Author:
Mark Harrison <mark@omniti.com>
git-committer:
Mark Harrison <mark@omniti.com> 1266524172 +0000
git-parent:

[f8f9000875618fecf39409e438860bb3b6775326]

git-author:
Mark Harrison <mark@omniti.com> 1266524172 +0000
Message:

Simple basic auth support

git-svn-id: https://labs.omniti.com/resmon/trunk@239 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lib/Resmon/Config.pm

    rd11ab61 rd5d5b49  
    8181        next; 
    8282      } 
     83      elsif(/\S*AUTHUSER\s+(\S+)\s*;\s*/) { 
     84        $self->{authuser} = $1; 
     85        next; 
     86      } 
     87      elsif(/\S*AUTHPASS\s+(\S+)\s*;\s*/) { 
     88        $self->{authpass} = $1; 
     89        next; 
     90      } 
    8391      else { 
    8492        die "Syntax Error on line $line\n"; 
  • lib/Resmon/Status.pm

    r0e5f64d rd5d5b49  
    306306sub service { 
    307307  my $self = shift; 
    308   my ($client, $req, $proto, $snip) = @_; 
     308  my ($client, $req, $proto, $snip, $authuser, $authpass) = @_; 
    309309  my $state = $self->get_shared_state(); 
    310   if($req eq '/' or $req eq '/status') { 
     310  if ($self->{authuser} ne "" && 
     311        ($authuser ne $self->{authuser} || $authpass ne $self->{authpass})) { 
     312      my $response = "<html><head><title>Password required</title></head>" . 
     313        "<body><h1>Password required</h1></body></html>"; 
     314      $client->print(http_header(401, length($response), 'text/html', $snip, 
     315          "WWW-Authenticate: Basic realm=\"Resmon\"\n")); 
     316      $client->print($response . "\r\n"); 
     317      return; 
     318  } elsif($req eq '/' or $req eq '/status') { 
    311319    my $response .= $self->dump_xml(); 
    312320    $client->print(http_header(200, length($response), 'text/xml', $snip)); 
     
    369377  my $type = shift || 'text/xml'; 
    370378  my $close_connection = shift || 1; 
     379  my $extra_headers = shift; 
    371380  return qq^HTTP/1.0 $code OK 
    372381Server: resmon 
     
    374383    (($close_connection || !$len) ? "Connection: close\n" : "") . 
    375384qq^Content-Type: $type; charset=utf-8 
    376  
     385^ . $extra_headers . qq^ 
    377386^; 
     387} 
     388sub base64_decode($) { 
     389    # Base64 decoding for basic auth 
     390    # We cheat when doing the decoding - perl can do uudecoding using unpack - 
     391    # so we just convert to uuencoded text and decode that. 
     392    my $enc = shift; 
     393    if (length($enc) % 4 != 0) { return "" } # Length should be multiple of 4 
     394    $enc =~ tr#A-Za-z0-9+/=##cd; # Ignore any invalid characters 
     395    $enc =~ tr#A-Za-z0-9+/=# -_#d; # Convert base64 to uuencode alphabet and 
     396                                   # strip padding 
     397    if (length($enc) > 63) { return "" }; # Only support up to 63 chars 
     398                                          # (one uuencoded line) 
     399    my $len = chr(32 + length($enc)*3/4); # uuencode has a length byte at the 
     400                                          # beginning 
     401    return unpack("u", $len.$enc); 
    378402} 
    379403sub serve_http_on { 
     
    381405  my $ip = shift; 
    382406  my $port = shift; 
     407  $self->{authuser} = shift; 
     408  $self->{authpass} = shift; 
    383409  $ip = INADDR_ANY if(!defined($ip) || $ip eq '' || $ip eq '*'); 
    384410  $port ||= 81; 
     
    413439        my $proto; 
    414440        my $close_connection; 
     441        my $authuser; 
     442        my $authpass; 
    415443        local $SIG{ALRM} = sub { die "timeout\n" }; 
    416444        eval { 
     
    434462              else { 
    435463                if(/^$/) { 
    436                   $self->service($client, $req, $proto, $close_connection); 
     464                  $self->service($client, $req, $proto, $close_connection, 
     465                    $authuser, $authpass); 
    437466                  last if ($close_connection); 
    438467                  alarm($KEEPALIVE_TIMEOUT); 
     
    447476                      $close_connection = 0; 
    448477                    } 
     478                  } 
     479                  if(/^Authorization: Basic (\S+)/) { 
     480                      my $dec = base64_decode($1); 
     481                      ($authuser, $authpass) = split /:/, $dec, 2 
    449482                  } 
    450483                } 
  • resmon

    r95026f2 rd5d5b49  
    118118$status = Resmon::Status->new($config->{statusfile}); 
    119119$status->open(); 
    120 $status->serve_http_on($config->{interface}, $config->{port}) 
     120$status->serve_http_on($config->{interface}, $config->{port}, 
     121                       $config->{authuser}, $config->{authpass}) 
    121122  if($config->{port}); 
    122123