Index: trac/core.py
===================================================================
--- trac/core.py	(revision 949)
+++ trac/core.py	(working copy)
@@ -291,17 +291,30 @@
 
     def end_headers(self):
         raise RuntimeError, 'Virtual method not implemented'
+        
+    def send_cookie(self, cookie):
+        cookie = cookie.output(header='')
+        if len(cookie):
+            self.send_header('Set-Cookie', cookie)
+            
+    def send_cache_headers(self):
+        self.send_header('Pragma', 'no-cache')
+        self.send_header('Cache-control', 'no-cache')
+        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
+            
+    def reauthorize(self, url):
+        self.send_response(401)
+        self.send_header('WWW-Authenticate', 'Basic realm="Trac"')
+        self.send_cookie(self.outcookie)
+        self.end_headers()
+        self.write('Reauthorizing...')
 
     def redirect(self, url):
         self.send_response(302)
         self.send_header('Location', url)
         self.send_header('Content-Type', 'text/plain')
-        self.send_header('Pragma', 'no-cache')
-        self.send_header('Cache-control', 'no-cache')
-        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
-        cookie = self.outcookie.output(header='')
-        if len(cookie):
-            self.send_header('Set-Cookie', cookie)
+        self.send_cache_headers()
+        self.send_cookie(self.outcookie)
         self.end_headers()
         self.write('Redirecting...')
         raise RedirectException()
@@ -319,13 +332,10 @@
             cs.parseFile(filename)
         data = cs.render()
         self.send_response(response)
-        self.send_header('Cache-control', 'no-cache')
-        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
+        self.send_cache_headers()
         self.send_header('Content-Type', content_type + ';charset=utf-8')
         self.send_header('Content-Length', len(data))
-        cookie = self.outcookie.output(header='')
-        if len(cookie):
-            self.send_header('Set-Cookie', cookie)
+        self.send_cookie(self.outcookie)
         self.end_headers()
         if self.command != 'HEAD':
             self.write(data)
@@ -399,8 +409,9 @@
     Wiki.populate_page_dict(database, env)
 
     authenticator = auth.Authenticator(database, req)
+    logged_out = False
     if path_info == '/logout':
-        authenticator.logout()
+        authenticator.logout(req)
         referer = req.get_header('Referer')
         if referer[0:len(req.base_url)] != req.base_url:
             # only redirect to referer if the latter is from the same instance
@@ -410,16 +421,20 @@
         except RedirectException:
             pass
     elif req.remote_user and authenticator.authname == 'anonymous':
-        auth_cookie = authenticator.login(req)
+        logged_out = authenticator.login(req)
     if path_info == '/login':
-        referer = req.get_header('Referer')
-        if referer[0:len(req.base_url)] != req.base_url:
-            # only redirect to referer if the latter is from the same instance
-            referer = None
-        try:
-            req.redirect(referer or env.href.wiki())
-        except RedirectException:
-            pass
+        if logged_out:
+            req.reauthorize()
+            return
+        else:
+            referer = req.get_header('Referer')
+            if not referer.startswith(req.base_url):
+                # only redirect to referer if the latter is from the same instance
+                referer = env.href.wiki()
+            try:
+                req.redirect(referer)
+            except RedirectException:
+                pass
     req.authname = authenticator.authname
 
     newsession = args.has_key('newsession') and args['newsession']
Index: trac/auth.py
===================================================================
--- trac/auth.py	(revision 949)
+++ trac/auth.py	(working copy)
@@ -37,6 +37,14 @@
                 self.authname = cursor.fetchone()[0]
 
     def login(self, req):
+        try:
+            if req.incookie['trac_auth'].value == 'logout':
+                req.outcookie['trac_auth'] = 'ok'
+                req.outcookie['trac_auth']['path'] = req.cgi_location
+                return True
+        except:
+            pass
+        
         cursor = self.db.cursor ()
         cookie = util.hex_entropy()
         cursor.execute ("INSERT INTO auth_cookie (cookie, name, ipnr, time)" +
@@ -48,8 +56,11 @@
         req.outcookie['trac_auth'] = cookie
         req.outcookie['trac_auth']['path'] = req.cgi_location
 
-    def logout(self):
+    def logout(self, req):
         cursor = self.db.cursor ()
         cursor.execute ("DELETE FROM auth_cookie WHERE name=%s",
                         self.authname)
         self.db.commit ()
+        req.outcookie['trac_auth'] = 'logout'
+        req.outcookie['trac_auth']['path'] = req.cgi_location
+        

