X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=cgi-auth-flexible.pm;h=a141877df270f8c6a111fe125ffd85230ad50b57;hb=ca51f3c2135502ea61e79f271d88c45d7d05bdcd;hp=6d1c0e8577fb79dab7fb2ed2e99d735e48d2aa66;hpb=c7fda90f0be4c210a36d13f0d20ef7f3f0da95e3;p=cgi-auth-flexible.git diff --git a/cgi-auth-flexible.pm b/cgi-auth-flexible.pm index 6d1c0e8..a141877 100644 --- a/cgi-auth-flexible.pm +++ b/cgi-auth-flexible.pm @@ -787,21 +787,23 @@ sub _check_divert_core ($) { die if $srcdump =~ m/\W/; return ({ Kind => 'SRCDUMP-'.uc $srcdump, Message => undef, - CookieSecret => undef, + _CookieRaw => undef, Params => { } }); } - my $cooks = $r->_ch('get_cookie'); + my $cooksraw = $r->_ch('get_cookie'); + my $cooks = $r->_unblind($cooksraw); if ($r->{S}{encrypted_only} && !$r->_ch('is_https')) { return ({ Kind => 'REDIRECT-HTTPS', Message => $r->_gt("Redirecting to secure server..."), - CookieSecret => undef, + _CookieRaw => undef, Params => { } }); } my $meth = $r->_ch('get_method'); - my $parmh = $r->_rp('assoc_param_name'); + my $parmhraw = $r->_rp('assoc_param_name'); + my $parmh = $r->_unblind($parmhraw); my $cookh = defined $cooks ? $r->hash($cooks) : undef; my ($cookt,$cooku) = $r->_identify($cookh, $cooks); @@ -818,8 +820,10 @@ sub _check_divert_core ($) { $r->_db_revoke($parmh); return ({ Kind => 'REDIRECT-LOGGEDOUT', Message => $r->_gt("Logging out..."), - CookieSecret => '', - Params => { } }); + _CookieRaw => '', + Params => { + $r->{S}{loggedout_param_names}[0] => [ 1 ], + } }); } if ($r->_ch('is_loggedout')) { die unless $meth eq 'GET'; @@ -827,7 +831,7 @@ sub _check_divert_core ($) { die if $parmt; return ({ Kind => 'SMALLPAGE-LOGGEDOUT', Message => $r->_gt("You have been logged out."), - CookieSecret => '', + _CookieRaw => '', Params => { } }); } if ($r->_ch('is_login')) { @@ -838,7 +842,7 @@ sub _check_divert_core ($) { Message => $r->_gt("You do not seem to have cookies". " enabled. You must enable cookies". " as we use them for login."), - CookieSecret => $r->_fresh_secret(), + _CookieRaw => $r->_fresh_secret(), Params => $r->_chain_params() }) } if (!$cookt || $cookt eq 'n' || $cookh ne $parmh) { @@ -846,7 +850,7 @@ sub _check_divert_core ($) { return ({ Kind => 'LOGIN-STALE', Message => $r->_gt("Stale session;". " you need to log in again."), - CookieSecret => $r->_fresh_secret(), + _CookieRaw => $r->_fresh_secret(), Params => { } }) } die unless $parmt eq 't' || $parmt eq 'y'; @@ -856,13 +860,13 @@ sub _check_divert_core ($) { if !$login_errormessage; return ({ Kind => 'LOGIN-BAD', Message => $login_errormessage, - CookieSecret => $cooks, + _CookieRaw => $cooks, Params => $r->_chain_params() }) } $r->_db_record_login_ok($parmh,$username); return ({ Kind => 'REDIRECT-LOGGEDIN', Message => $r->_gt("Logging in..."), - CookieSecret => $cooks, + _CookieRaw => $cooks, Params => $r->_chain_params() }); } if ($cookt eq 't') { @@ -882,13 +886,13 @@ sub _check_divert_core ($) { if ($meth eq 'GET') { return ({ Kind => 'LOGIN-INCOMINGLINK', Message => $r->_gt("You need to log in."), - CookieSecret => $news, + _CookieRaw => $news, Params => $r->_chain_params() }); } else { $r->_db_revoke($parmh); return ({ Kind => 'LOGIN-FRESH', Message => $r->_gt("You need to log in."), - CookieSecret => $news, + _CookieRaw => $news, Params => { } }); } } @@ -897,7 +901,7 @@ sub _check_divert_core ($) { if ($meth ne 'POST') { return ({ Kind => 'MAINPAGEONLY', Message => $r->_gt('Entering via cross-site link.'), - CookieSecret => $cooks, + _CookieRaw => $cooks, Params => { } }); # NB caller must then ignore params & path! # if this is too hard they can spit out a small form @@ -911,7 +915,7 @@ sub _check_divert_core ($) { die unless $cookh eq $parmh; } $r->{ParmT} = $parmt; - $r->{AssocSecret} = $cooks; + $r->{AssocRaw} = $cooks; $r->{UserOK} = $cooku; #print STDERR "C-D-C OK\n"; return undef; @@ -1030,6 +1034,15 @@ sub check_divert ($) { my $dbh = $r->{Dbh}; $r->{Divert} = $r->_db_transaction(sub { $r->_check_divert_core(); }); $dbh->commit(); + + my $cookraw = $r->{_CookieRaw}; + $r->{CookieSecret} = $r->_blind($cookraw); + if ($cookraw) { + $r->{Params}{$r->{S}{assoc_param_name}} = [ + $r->_blind($r->hash($cookraw)) + ]; + } + $r->_debug(Data::Dumper->Dump([$r->{Divert}],[qw(divert)])); return $r->{Divert}; } @@ -1091,9 +1104,6 @@ sub check_ok ($) { # for redirects, we honour stored Params and Cookie, # as we would for non-divert if ($kind eq 'REDIRECT-LOGGEDOUT') { - $params->{$r->{S}{loggedout_param_names}[0]} = [ 1 ]; - } elsif ($kind eq 'REDIRECT-LOGOUT') { - $params->{$r->{S}{logout_param_names}[0]} = [ 1 ]; } elsif ($kind =~ m/REDIRECT-(?:LOGGEDIN|HTTPS)/) { } else { die; @@ -1109,10 +1119,6 @@ sub check_ok ($) { return 0; } - if (defined $cookiesecret) { - $params->{$r->{S}{assoc_param_name}} = [ $r->hash($cookiesecret) ]; - } - my ($title, @body); if ($kind =~ m/^LOGIN-/) { $title = $r->_gt('Login'); @@ -1155,6 +1161,40 @@ sub _random ($$) { return $out; } +sub _blind_len ($$) { + my ($r, $str) = @_; + return length($str =~ y/0-9a-f//cdr); +} + +sub _blind_combine ($$$) { + my ($r, $in, $mask) = @_; + my @mask = split //, $mask; + $in =~ s{[0-9a-f]}{ + my $m = shift @mask; + sprintf "%x", hex($m) ^ hex($&); + }ge; + return $in; +} + +sub _blind ($$) { + my ($r, $in) = @_; + return $in unless $in; + my $l = $r->_blind_len($in); + my $mask = $r->_random(($l+1)>>1); + $mask = substr $mask, 0, $l; + my $blound = $r->_blind_combine($in, $mask); + return "$blound/$mask"; +} + +sub _unblind ($$) { + my ($r, $in) = @_; + return $in unless $in; + my ($blound,$mask) = ($in =~ m#^(.*)/([0-9a-f]+)$#) or die "$in ?"; + my $l = $r->_blind_len($blound); + $l == length($mask) or die "$in ?"; + return $r->_blind_combine($blound, $mask); +} + sub _random_key ($) { my ($r) = @_; #print STDERR "_random_key\n"; @@ -1336,13 +1376,13 @@ sub check_nonpage ($$) { sub secret_cookie_val ($) { my ($r) = @_; $r->_assert_checked(); - return defined $r->{AssocSecret} ? $r->{AssocSecret} : ''; + return defined $r->{AssocRaw} ? $r->_blind($r->{AssocRaw}) : ''; } sub secret_hidden_val ($) { my ($r) = @_; $r->_assert_checked(); - return defined $r->{AssocSecret} ? $r->hash($r->{AssocSecret}) : ''; + return defined $r->{AssocRaw} ? $r->_blind($r->hash($r->{AssocRaw})) : ''; } sub secret_hidden_html ($) {