;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/hunchentoot/session.lisp,v 1.11 2007/06/04 19:24:12 edi Exp $ ;;; Copyright (c) 2004-2007, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :hunchentoot) (defgeneric realm (request) (:documentation "Returns the realm under which the request has been sent. A realm is used to group resources under a common 'place', and is used for registered web applications to have different or common sessions for a give user.")) (defgeneric (setf realm) (value request) (:documentation "Sets the realm under which the request has been sent, where value is the realm name. A realm is used to group resources under a common 'place', and is used for registered web applications to have different or common sessions for a give user.")) (defmethod realm ((request request)) (aux-request-value 'realm request)) (defmethod (setf realm) (value (request request)) (setf (aux-request-value 'realm request) value) (session-realm-verify request)) ;;;------------------------------------------------------------------------------- (defclass session () ((session-id :initform (get-next-session-id) :reader session-id :type integer :documentation "The unique ID \(an INTEGER) of the session.") (session-realm :initform (realm *request*) :reader session-realm :documentation "Defines a realm for this session. A realm is injected by *request* aux parameter, and is used to group resources that will share this session object.") (session-string :reader session-string :documentation "The session strings encodes enough data to safely retrieve this session. It is sent to the browser as a cookie value or as a GET parameter.") (user-agent :initform (user-agent *request*) :reader session-user-agent :documentation "The incoming 'User-Agent' header that was sent when this session was created.") (remote-addr :initform (real-remote-addr *request*) :reader session-remote-addr :documentation "The remote IP address of the client when this sessions was started as returned by REAL-REMOTE-ADDR.") (session-start :initform (get-universal-time) :reader session-start :documentation "The time this session was started.") (last-click :initform (get-universal-time) :reader session-last-click :documentation "The last time this session was used.") (session-data :initarg :session-data :initform nil :reader session-data :documentation "Data associated with this session - see SESSION-VALUE.") (session-counter :initform 0 :reader session-counter :documentation "The number of times this session has been used.") (max-time :initarg :max-time :initform *session-max-time* :accessor session-max-time :type fixnum :documentation "The time \(in seconds) after which this session expires if it's not used.")) (:documentation "SESSION objects are automatically maintained by Hunchentoot. They should not be created explicitly with MAKE-INSTANCE but implicitly with START-SESSION. Note that SESSION objects can only be created when the special variable *REQUEST* is bound to a REQUEST object.")) (defun encode-session-string (id user-agent remote-addr start realm) "Create a uniquely encoded session string based on the values ID, USER-AGENT, REMOTE-ADDR, START and REALM" ;; *SESSION-SECRET* is used twice due to known theoretical ;; vulnerabilities of MD5 encoding (md5-hex (concatenate 'string *session-secret* (md5-hex (format nil "~A~A~@[~A~]~@[~A~]~A~@[~A~]" *session-secret* id (and *use-user-agent-for-sessions* user-agent) (and *use-remote-addr-for-sessions* remote-addr) start realm))))) (defun stringify-session (session) "Creates a string representing the SESSION object SESSION. See ENCODE-SESSION-STRING." (encode-session-string (session-id session) (session-user-agent session) (session-remote-addr session) (session-start session) (session-realm session))) (defun session-realm-verify (request) "Once a session is verified for a given user this function verifies that it belongs to the request realm, so that session and request realm must be the same." (when (session request) (let ((req-realm (realm request)) (realm (session-realm (session request)))) (when (string-not-equal req-realm realm) (log-message :info "2) $$$$$~a$$$$$" (aux-request-value 'realm request)) (log-message :info "#####~a ~a#####" req-realm realm) (setf (session request) nil) (setf *session* nil))))) (defun session-verify (request) "Tries to get a session identifier from the cookies \(or alternatively from the GET parameters) sent by the client. This identifier is then checked for validity against the REQUEST object REQUEST. On success the corresponding session object \(if not too old) is returned \(and updated). Otherwise NIL is returned." (let ((session-identifier (or (cookie-in *session-cookie-name* request) (get-parameter *session-cookie-name* request)))) (unless (and session-identifier (stringp session-identifier) (plusp (length session-identifier))) (return-from session-verify nil)) (destructuring-bind (id-string session-string) (split ":" session-identifier :limit 2) (let* ((id (and (scan "^\\d+$" id-string) (parse-integer id-string :junk-allowed t))) (session (and id (get-stored-session id))) (user-agent (user-agent request)) (remote-addr (remote-addr request)) (realm (when session (session-realm session)))) (unless (and session session-string (string= session-string (session-string session)) (string= session-string (encode-session-string id user-agent (real-remote-addr request) (session-start session) realm))) (when *reply* (cond ((null session) (log-message :notice "No session for session identifier '~A' \(User-Agent: '~A', IP: '~A', REALM: '~A')" session-identifier user-agent remote-addr realm)) (t (log-message :warning "Fake session identifier '~A' \(User-Agent: '~A', IP: '~A', REALM: '~A')" session-identifier user-agent remote-addr realm)))) (when session (remove-session session)) (return-from session-verify nil)) (incf (slot-value session 'session-counter)) (setf (slot-value session 'last-click) (get-universal-time)) session)))) (defun start-session (&optional (path "/")) "Returns the current SESSION object. If there is no current session, creates one and updates the corresponding data structures. In this case the function will also send a session cookie to the browser. This function slightly differs from standard hunchentoot implementation because it can bound a session to a specific url inside the same server instance. The path optional parameter has sense when the cookies are enabled, and bounds resources under the given path to a specific session" (count-session-usage) (let ((session (session *request*))) (when session (return-from start-session session)) (setf session (make-instance 'session) (session *request*) session) (with-lock (*session-data-lock*) (setq *session-data* (acons (session-id session) session *session-data*))) (set-cookie *session-cookie-name* :value (session-cookie-value session) :path path) (setq *session* session))) ;;;--------------------------- dispatchers ---------------------------------------------- (defun create-prefix-dispatcher (prefix page-function &optional (realm "Hunchentoot")) "Creates a dispatch function which will dispatch to the function denoted by PAGE-FUNCTION if the file name of the current request starts with the string PREFIX. The optional parameter realm is a string that identifies the realm under which the request is displatching. A realm is used to group resources under a common 'place', and is used for registered web applications to have different or common sessions for a give user." (lambda (request) (let ((mismatch (mismatch (script-name request) prefix :test #'char=))) (when (and (or (null mismatch) (>= mismatch (length prefix))) page-function) (setf (realm request) realm) page-function)))) (defun create-regex-dispatcher (regex page-function &optional (realm "Hunchentoot")) "Creates a dispatch function whipch will dispatch to the function denoted by PAGE-FUNCTION if the file name of the current request matches the CL-PPCRE regular expression REGEX. The optional parameter realm is a string that identifies the realm under which the request is displatching. A realm is used to group resources under a common 'place', and is used for registered web applications to have different or common sessions for a give user." (let ((scanner (create-scanner regex))) (lambda (request) (when (and (scan scanner (script-name request)) page-function) (setf (realm request) realm) page-function))))