Re: [Nagios-devel] Fix: login as new user

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

Re: [Nagios-devel] Fix: login as new user

Post by Guest »

On Wed, Feb 12, 2003 at 15:45:04 -0600, Carroll, Jim P [Contractor] wrote:

> Nope, no joy.

I noticed some other browsers having problems, so I took another approach.
This time no cookies, but a db-file on the server identifying a session by
REMOTE_IP and HTTP_USER_AGENT. The required modules are in the standard
Perl distribution. Does it work for you?

Robert


#!/usr/bin/perl -w

use strict;
use Fcntl;
use DB_File;

my $realm = "Nagios Access"; # Should be the same as in .htaccess
my $dbfile = "/usr/local/nagios/var/rw/authcache.db";

my $browser = $ENV{'HTTP_USER_AGENT'} || "Unknown browser";
my $ip = $ENV{'REMOTE_ADDR'} || "Unknown IP";
my $user = $ENV{'REMOTE_USER'} or &print_auth_ok;

my $db = tie my %db, 'DB_File', $dbfile, O_CREAT|O_RDWR, 0640 or &print_error;
my $id = "$ip - $browser";

if (my $lastuser = db_get($id)) {
if ($user eq $lastuser) {
&print_auth_required;
} else {
db_del($id);
&print_auth_ok;
}
} else {
db_put($id, $user);
&print_auth_ok;
}

sub print_auth_required {
print "Content-type: http/send-as-is\n";
print "Status: 401 Authorization Required\n";
print "WWW-Authenticate: Basic realm=\"$realm\"\n";
print "Content-type: text/html\n";
print "\n";
print "Authorization is required\n";
exit 0;
}

sub print_auth_ok {
print "Content-type: text/html\n";
print "Expires: now\n";
print "\n";
if (defined $user) {
print "You are now logged in as $user\n";
print "Click again if you want to login as another user.\n";
} else {
print "No authentication required\n";
}
exit 0;
}

sub print_error {
print "Content-type: text/html\n";
print "Expires: now\n";
print "\n";
print "Internal error\n";
exit 0;
}

sub db_get {
my ($key) = @_;
my $val;
$db->get($key, $val) == 1 ? return undef : return $val;
}

sub db_put {
my ($key, $val) = @_;
$db->put($key, $val);
$db->sync();
return 1;
}

sub db_del {
my ($key) = @_;
$db->del($key);
$db->sync();
return 1;
}





This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]
Locked