]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/CGI.pm
response
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
index dac522eea470a544248c9c77406009b27d4d9997..07369ac1076e748f8cd95e1aab5c2354a33877c8 100644 (file)
@@ -9,19 +9,20 @@ use IkiWiki::UserInfo;
 use open qw{:utf8 :std};
 use Encode;
 
-sub printheader ($) { #{{{
+sub printheader ($) {
        my $session=shift;
        
        if ($config{sslcookie}) {
                print $session->header(-charset => 'utf-8',
                        -cookie => $session->cookie(-httponly => 1, -secure => 1));
-       } else {
+       }
+       else {
                print $session->header(-charset => 'utf-8',
                        -cookie => $session->cookie(-httponly => 1));
        }
-} #}}}
+}
 
-sub showform ($$$$;@) { #{{{
+sub showform ($$$$;@) {
        my $form=shift;
        my $buttons=shift;
        my $session=shift;
@@ -38,9 +39,10 @@ sub showform ($$$$;@) { #{{{
        print misctemplate($form->title, $form->render(submit => $buttons), @_);
 }
 
-sub redirect ($$) { #{{{
+sub redirect ($$) {
        my $q=shift;
-       my $url=shift;
+       eval q{use URI};
+       my $url=URI->new(shift);
        if (! $config{w3mmode}) {
                print $q->redirect($url);
        }
@@ -48,33 +50,34 @@ sub redirect ($$) { #{{{
                print "Content-type: text/plain\n";
                print "W3m-control: GOTO $url\n\n";
        }
-} #}}}
+}
 
-sub decode_cgi_utf8 ($) { #{{{
-       # decode_form_utf8 method is needed for 5.10
+sub decode_cgi_utf8 ($) {
+       # decode_form_utf8 method is needed for 5.01
        if ($] < 5.01) {
                my $cgi = shift;
                foreach my $f ($cgi->param) {
                        $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
                }
        }
-} #}}}
+}
 
-sub decode_form_utf8 ($) { #{{{
+sub decode_form_utf8 ($) {
        if ($] >= 5.01) {
                my $form = shift;
                foreach my $f ($form->field) {
+                       my @value=map { decode_utf8($_) } $form->field($f);
                        $form->field(name  => $f,
-                                    value => decode_utf8($form->field($f)),
+                                    value => \@value,
                                     force => 1,
                        );
                }
        }
-} #}}}
+}
 
 # Check if the user is signed in. If not, redirect to the signin form and
 # save their place to return to later.
-sub needsignin ($$) { #{{{
+sub needsignin ($$) {
        my $q=shift;
        my $session=shift;
 
@@ -85,9 +88,9 @@ sub needsignin ($$) { #{{{
                cgi_savesession($session);
                exit;
        }
-} #}}} 
+}
 
-sub cgi_signin ($$) { #{{{
+sub cgi_signin ($$) {
        my $q=shift;
        my $session=shift;
 
@@ -127,9 +130,9 @@ sub cgi_signin ($$) { #{{{
        }
 
        showform($form, $buttons, $session, $q);
-} #}}}
+}
 
-sub cgi_postsignin ($$) { #{{{
+sub cgi_postsignin ($$) {
        my $q=shift;
        my $session=shift;
        
@@ -142,11 +145,16 @@ sub cgi_postsignin ($$) { #{{{
                exit;
        }
        else {
-               error(gettext("login failed, perhaps you need to turn on cookies?"));
+               if ($config{sslcookie} && ! $q->https()) {
+                       error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
+               }
+               else {
+                       error(gettext("login failed, perhaps you need to turn on cookies?"));
+               }
        }
-} #}}}
+}
 
-sub cgi_prefs ($$) { #{{{
+sub cgi_prefs ($$) {
        my $q=shift;
        my $session=shift;
 
@@ -203,25 +211,9 @@ sub cgi_prefs ($$) { #{{{
        
        my $user_name=$session->param("name");
 
-       # XXX deprecated, should be removed eventually
-       $form->field(name => "banned_users", size => 50, fieldset => "admin");
-       if (! is_admin($user_name)) {
-               $form->field(name => "banned_users", type => "hidden");
-       }
        if (! $form->submitted) {
                $form->field(name => "email", force => 1,
                        value => userinfo_get($user_name, "email"));
-               if (is_admin($user_name)) {
-                       my $value=join(" ", get_banned_users());
-                       if (length $value) {
-                               $form->field(name => "banned_users", force => 1,
-                                       value => join(" ", get_banned_users()),
-                                       comment => "deprecated; please move to banned_users in setup file");
-                       }
-                       else {
-                               $form->field(name => "banned_users", type => "hidden");
-                       }
-               }
        }
        
        if ($form->submitted eq 'Logout') {
@@ -239,48 +231,66 @@ sub cgi_prefs ($$) { #{{{
                                error("failed to set email");
                }
 
-               # XXX deprecated, should be removed eventually
-               if (is_admin($user_name)) {
-                       set_banned_users(grep { ! is_admin($_) }
-                                       split(' ',
-                                               $form->field("banned_users"))) ||
-                               error("failed saving changes");
-                       if (! length $form->field("banned_users")) {
-                               $form->field(name => "banned_users", type => "hidden");
-                       }
-               }
-
                $form->text(gettext("Preferences saved."));
        }
        
        showform($form, $buttons, $session, $q);
-} #}}}
+}
+
+sub cgi_custom_failure ($$$) {
+       my $q=shift;
+       my $httpstatus=shift;
+       my $message=shift;
 
-sub check_banned ($$) { #{{{
+       print $q->header(
+               -status => $httpstatus,
+               -charset => 'utf-8',
+       );
+       print $message;
+
+       # Internet Explod^Hrer won't show custom 404 responses
+       # unless they're >= 512 bytes
+       print ' ' x 512;
+
+       exit;
+}
+
+sub check_banned ($$) {
        my $q=shift;
        my $session=shift;
 
+       my $banned=0;
        my $name=$session->param("name");
-       if (defined $name) {
-               # XXX banned in userinfo is deprecated, should be removed
-               # eventually, and only banned_users be checked.
-               if (userinfo_get($session->param("name"), "banned") ||
-                   grep { $name eq $_ } @{$config{banned_users}}) {
-                       print $q->header(-status => "403 Forbidden");
-                       $session->delete();
-                       print gettext("You are banned.");
-                       cgi_savesession($session);
-                       exit;
+       if (defined $name && 
+           grep { $name eq $_ } @{$config{banned_users}}) {
+               $banned=1;
+       }
+
+       foreach my $b (@{$config{banned_users}}) {
+               if (pagespec_match("", $b,
+                       ip => $ENV{REMOTE_ADDR},
+                       name => defined $name ? $name : "",
+               )) {
+                       $banned=1;
+                       last;
                }
        }
+
+       if ($banned) {
+               $session->delete();
+               cgi_savesession($session);
+               cgi_custom_failure(
+                       $q, "403 Forbidden",
+                       gettext("You are banned."));
+       }
 }
 
-sub cgi_getsession ($) { #{{{
+sub cgi_getsession ($) {
        my $q=shift;
 
-       eval q{use CGI::Session};
+       eval q{use CGI::Session; use HTML::Entities};
        error($@) if $@;
-       CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
+       CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
        
        my $oldmask=umask(077);
        my $session = eval {
@@ -294,18 +304,34 @@ sub cgi_getsession ($) { #{{{
        umask($oldmask);
 
        return $session;
-} #}}}
+}
 
-sub cgi_savesession ($) { #{{{
+# To guard against CSRF, the user's session id (sid)
+# can be stored on a form. This function will check
+# (for logged in users) that the sid on the form matches
+# the session id in the cookie.
+sub checksessionexpiry ($$) {
+       my $q=shift;
+       my $session = shift;
+
+       if (defined $session->param("name")) {
+               my $sid=$q->param('sid');
+               if (! defined $sid || $sid ne $session->id) {
+                       error(gettext("Your login session has expired."));
+               }
+       }
+}
+
+sub cgi_savesession ($) {
        my $session=shift;
 
        # Force session flush with safe umask.
        my $oldmask=umask(077);
        $session->flush;
        umask($oldmask);
-} #}}}
+}
 
-sub cgi (;$$) { #{{{
+sub cgi (;$$) {
        my $q=shift;
        my $session=shift;
 
@@ -331,7 +357,7 @@ sub cgi (;$$) { #{{{
                        error("\"do\" parameter missing");
                }
        }
-       
+
        # Need to lock the wiki before getting a session.
        lockwiki();
        loadindex();
@@ -375,16 +401,16 @@ sub cgi (;$$) { #{{{
        else {
                error("unknown do parameter");
        }
-} #}}}
+}
 
 # Does not need to be called directly; all errors will go through here.
-sub cgierror ($) { #{{{
+sub cgierror ($) {
        my $message=shift;
 
        print "Content-type: text/html\n\n";
        print misctemplate(gettext("Error"),
                "<p class=\"error\">".gettext("Error").": $message</p>");
        die $@;
-} #}}}
+}
 
 1