]> code.delx.au - gnu-emacs/blob - lisp/url/url-cookie.el
(zenirc, zenirc-send-line): Declare as functions.
[gnu-emacs] / lisp / url / url-cookie.el
1 ;;; url-cookie.el --- Netscape Cookie support
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Keywords: comm, data, processes, hypermedia
7
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14 ;;
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'timezone)
30 (require 'url-util)
31 (require 'url-parse)
32 (eval-when-compile (require 'cl))
33
34 ;; See http://home.netscape.com/newsref/std/cookie_spec.html for the
35 ;; 'open standard' defining this crap.
36
37 (defgroup url-cookie nil
38 "URL cookies."
39 :prefix "url-"
40 :prefix "url-cookie-"
41 :group 'url)
42
43 ;; A cookie is stored internally as a vector of 7 slots
44 ;; [ cookie NAME VALUE EXPIRES LOCALPART DOMAIN SECURE ]
45
46 (defstruct (url-cookie
47 (:constructor url-cookie-create)
48 (:copier nil)
49 ;; For compatibility with a previous version which did not use
50 ;; defstruct, and also in order to make sure that the printed
51 ;; representation does not depend on CL internals, we use an
52 ;; explicitly managed tag.
53 (:type vector))
54 (tag 'cookie :read-only t)
55 name value expires localpart domain secure)
56
57 (defvar url-cookie-storage nil "Where cookies are stored.")
58 (defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
59 (defcustom url-cookie-file nil
60 "File where cookies are stored on disk."
61 :type '(choice (const :tag "Default" :value nil) file)
62 :group 'url-file
63 :group 'url-cookie)
64
65 (defcustom url-cookie-confirmation nil
66 "If non-nil, confirmation by the user is required to accept HTTP cookies."
67 :type 'boolean
68 :group 'url-cookie)
69
70 (defcustom url-cookie-multiple-line nil
71 "If nil, HTTP requests put all cookies for the server on one line.
72 Some web servers, such as http://www.hotmail.com/, only accept cookies
73 when they are on one line. This is broken behavior, but just try
74 telling Microsoft that."
75 :type 'boolean
76 :group 'url-cookie)
77
78 (defvar url-cookies-changed-since-last-save nil
79 "Whether the cookies list has changed since the last save operation.")
80
81 (defun url-cookie-parse-file (&optional fname)
82 (setq fname (or fname url-cookie-file))
83 (condition-case ()
84 (load fname nil t)
85 (error
86 ;; It's completely normal for the cookies file not to exist yet.
87 ;; (message "Could not load cookie file %s" fname)
88 )))
89
90 ;; check-declare does not handle defstruct.
91 ;;;(declare-function url-cookie-p "url-cookie")
92
93 (defun url-cookie-clean-up (&optional secure)
94 (let* (
95 (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
96 (val (symbol-value var))
97 (cur nil)
98 (new nil)
99 (cookies nil)
100 (cur-cookie nil)
101 (new-cookies nil)
102 )
103 (while val
104 (setq cur (car val)
105 val (cdr val)
106 new-cookies nil
107 cookies (cdr cur))
108 (while cookies
109 (setq cur-cookie (car cookies)
110 cookies (cdr cookies))
111 (if (or (not (url-cookie-p cur-cookie))
112 (url-cookie-expired-p cur-cookie)
113 (null (url-cookie-expires cur-cookie)))
114 nil
115 (setq new-cookies (cons cur-cookie new-cookies))))
116 (if (not new-cookies)
117 nil
118 (setcdr cur new-cookies)
119 (setq new (cons cur new))))
120 (set var new)))
121
122 (defun url-cookie-write-file (&optional fname)
123 (setq fname (or fname url-cookie-file))
124 (unless (file-directory-p (file-name-directory fname))
125 (ignore-errors (make-directory (file-name-directory fname))))
126 (cond
127 ((not url-cookies-changed-since-last-save) nil)
128 ((not (file-writable-p fname))
129 (message "Cookies file %s (see variable `url-cookie-file') is unwritable." fname))
130 (t
131 (url-cookie-clean-up)
132 (url-cookie-clean-up t)
133 (with-current-buffer (get-buffer-create " *cookies*")
134 (erase-buffer)
135 (fundamental-mode)
136 (insert ";; Emacs-W3 HTTP cookies file\n"
137 ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
138 "(setq url-cookie-storage\n '")
139 (pp url-cookie-storage (current-buffer))
140 (insert ")\n(setq url-cookie-secure-storage\n '")
141 (pp url-cookie-secure-storage (current-buffer))
142 (insert ")\n")
143 (insert "\f\n;; Local Variables:\n"
144 ";; version-control: never\n"
145 ";; no-byte-compile: t\n"
146 ";; End:\n")
147 (set (make-local-variable 'version-control) 'never)
148 (write-file fname)
149 (setq url-cookies-changed-since-last-save nil)
150 (kill-buffer (current-buffer))))))
151
152 (defun url-cookie-store (name value &optional expires domain localpart secure)
153 "Store a netscape-style cookie."
154 (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage))
155 (tmp storage)
156 (cur nil)
157 (found-domain nil))
158
159 ;; First, look for a matching domain
160 (setq found-domain (assoc domain storage))
161
162 (if found-domain
163 ;; Need to either stick the new cookie in existing domain storage
164 ;; or possibly replace an existing cookie if the names match.
165 (progn
166 (setq storage (cdr found-domain)
167 tmp nil)
168 (while storage
169 (setq cur (car storage)
170 storage (cdr storage))
171 (if (and (equal localpart (url-cookie-localpart cur))
172 (equal name (url-cookie-name cur)))
173 (progn
174 (setf (url-cookie-expires cur) expires)
175 (setf (url-cookie-value cur) value)
176 (setq tmp t))))
177 (if (not tmp)
178 ;; New cookie
179 (setcdr found-domain (cons
180 (url-cookie-create :name name
181 :value value
182 :expires expires
183 :domain domain
184 :localpart localpart
185 :secure secure)
186 (cdr found-domain)))))
187 ;; Need to add a new top-level domain
188 (setq tmp (url-cookie-create :name name
189 :value value
190 :expires expires
191 :domain domain
192 :localpart localpart
193 :secure secure))
194 (cond
195 (storage
196 (setcdr storage (cons (list domain tmp) (cdr storage))))
197 (secure
198 (setq url-cookie-secure-storage (list (list domain tmp))))
199 (t
200 (setq url-cookie-storage (list (list domain tmp))))))))
201
202 (defun url-cookie-expired-p (cookie)
203 (let* (
204 (exp (url-cookie-expires cookie))
205 (cur-date (and exp (timezone-parse-date (current-time-string))))
206 (exp-date (and exp (timezone-parse-date exp)))
207 (cur-greg (and cur-date (timezone-absolute-from-gregorian
208 (string-to-number (aref cur-date 1))
209 (string-to-number (aref cur-date 2))
210 (string-to-number (aref cur-date 0)))))
211 (exp-greg (and exp (timezone-absolute-from-gregorian
212 (string-to-number (aref exp-date 1))
213 (string-to-number (aref exp-date 2))
214 (string-to-number (aref exp-date 0)))))
215 (diff-in-days (and exp (- cur-greg exp-greg)))
216 )
217 (cond
218 ((not exp) nil) ; No expiry == expires at browser quit
219 ((< diff-in-days 0) nil) ; Expires sometime after today
220 ((> diff-in-days 0) t) ; Expired before today
221 (t ; Expires sometime today, check times
222 (let* ((cur-time (timezone-parse-time (aref cur-date 3)))
223 (exp-time (timezone-parse-time (aref exp-date 3)))
224 (cur-norm (+ (* 360 (string-to-number (aref cur-time 2)))
225 (* 60 (string-to-number (aref cur-time 1)))
226 (* 1 (string-to-number (aref cur-time 0)))))
227 (exp-norm (+ (* 360 (string-to-number (aref exp-time 2)))
228 (* 60 (string-to-number (aref exp-time 1)))
229 (* 1 (string-to-number (aref exp-time 0))))))
230 (> (- cur-norm exp-norm) 1))))))
231
232 (defun url-cookie-retrieve (host localpart &optional secure)
233 "Retrieve all the netscape-style cookies for a specified HOST and LOCALPART."
234 (let ((storage (if secure
235 (append url-cookie-secure-storage url-cookie-storage)
236 url-cookie-storage))
237 (case-fold-search t)
238 (cookies nil)
239 (cur nil)
240 (retval nil)
241 (localpart-regexp nil))
242 (while storage
243 (setq cur (car storage)
244 storage (cdr storage)
245 cookies (cdr cur))
246 (if (and (car cur)
247 (string-match
248 (concat "^.*"
249 (regexp-quote
250 ;; Remove the dot from wildcard domains
251 ;; before matching.
252 (if (eq ?. (aref (car cur) 0))
253 (substring (car cur) 1)
254 (car cur)))
255 "$") host))
256 ;; The domains match - a possible hit!
257 (while cookies
258 (setq cur (car cookies)
259 cookies (cdr cookies)
260 localpart-regexp (concat "^" (regexp-quote
261 (url-cookie-localpart cur))))
262 (if (and (string-match localpart-regexp localpart)
263 (not (url-cookie-expired-p cur)))
264 (setq retval (cons cur retval))))))
265 retval))
266
267 (defun url-cookie-generate-header-lines (host localpart secure)
268 (let* ((cookies (url-cookie-retrieve host localpart secure))
269 (retval nil)
270 (cur nil)
271 (chunk nil))
272 ;; Have to sort this for sending most specific cookies first
273 (setq cookies (and cookies
274 (sort cookies
275 (function
276 (lambda (x y)
277 (> (length (url-cookie-localpart x))
278 (length (url-cookie-localpart y))))))))
279 (while cookies
280 (setq cur (car cookies)
281 cookies (cdr cookies)
282 chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
283 retval (if (and url-cookie-multiple-line
284 (< 80 (+ (length retval) (length chunk) 4)))
285 (concat retval "\r\nCookie: " chunk)
286 (if retval
287 (concat retval "; " chunk)
288 (concat "Cookie: " chunk)))))
289 (if retval
290 (concat retval "\r\n")
291 "")))
292
293 (defvar url-cookie-two-dot-domains
294 (concat "\\.\\("
295 (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
296 "\\|")
297 "\\)$")
298 "A regexp of top level domains that only require two matching
299 '.'s in the domain name in order to set a cookie.")
300
301 (defcustom url-cookie-trusted-urls nil
302 "A list of regular expressions matching URLs to always accept cookies from."
303 :type '(repeat regexp)
304 :group 'url-cookie)
305
306 (defcustom url-cookie-untrusted-urls nil
307 "A list of regular expressions matching URLs to never accept cookies from."
308 :type '(repeat regexp)
309 :group 'url-cookie)
310
311 (defun url-cookie-host-can-set-p (host domain)
312 (let ((numdots 0)
313 (last nil)
314 (case-fold-search t)
315 (mindots 3))
316 (while (setq last (string-match "\\." domain last))
317 (setq numdots (1+ numdots)
318 last (1+ last)))
319 (if (string-match url-cookie-two-dot-domains domain)
320 (setq mindots 2))
321 (cond
322 ((string= host domain) ; Apparently netscape lets you do this
323 t)
324 ((>= numdots mindots) ; We have enough dots in domain name
325 ;; Need to check and make sure the host is actually _in_ the
326 ;; domain it wants to set a cookie for though.
327 (string-match (concat (regexp-quote
328 ;; Remove the dot from wildcard domains
329 ;; before matching.
330 (if (eq ?. (aref domain 0))
331 (substring domain 1)
332 domain))
333 "$") host))
334 (t
335 nil))))
336
337 (defun url-cookie-handle-set-cookie (str)
338 (setq url-cookies-changed-since-last-save t)
339 (let* ((args (url-parse-args str t))
340 (case-fold-search t)
341 (secure (and (assoc-string "secure" args t) t))
342 (domain (or (cdr-safe (assoc-string "domain" args t))
343 (url-host url-current-object)))
344 (current-url (url-view-url t))
345 (trusted url-cookie-trusted-urls)
346 (untrusted url-cookie-untrusted-urls)
347 (expires (cdr-safe (assoc-string "expires" args t)))
348 (localpart (or (cdr-safe (assoc-string "path" args t))
349 (file-name-directory
350 (url-filename url-current-object))))
351 (rest nil))
352 (while args
353 (if (not (member (downcase (car (car args)))
354 '("secure" "domain" "expires" "path")))
355 (setq rest (cons (car args) rest)))
356 (setq args (cdr args)))
357
358 ;; Sometimes we get dates that the timezone package cannot handle very
359 ;; gracefully - take care of this here, instead of in url-cookie-expired-p
360 ;; to speed things up.
361 (if (and expires
362 (string-match
363 (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
364 "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
365 expires))
366 (setq expires (concat (match-string 1 expires) " "
367 (match-string 2 expires) " "
368 (match-string 3 expires) " "
369 (match-string 4 expires) " ["
370 (match-string 5 expires) "]")))
371
372 ;; This one is for older Emacs/XEmacs variants that don't
373 ;; understand this format without tenths of a second in it.
374 ;; Wednesday, 30-Dec-2037 16:00:00 GMT
375 ;; - vs -
376 ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
377 (if (and expires
378 (string-match
379 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
380 expires))
381 (setq expires (concat (match-string 1 expires) "-" ; day
382 (match-string 2 expires) "-" ; month
383 (match-string 3 expires) " " ; year
384 (match-string 4 expires) ".00 " ; hour:minutes:seconds
385 (match-string 6 expires)))) ":" ; timezone
386
387 (while (consp trusted)
388 (if (string-match (car trusted) current-url)
389 (setq trusted (- (match-end 0) (match-beginning 0)))
390 (pop trusted)))
391 (while (consp untrusted)
392 (if (string-match (car untrusted) current-url)
393 (setq untrusted (- (match-end 0) (match-beginning 0)))
394 (pop untrusted)))
395 (if (and trusted untrusted)
396 ;; Choose the more specific match
397 (if (> trusted untrusted)
398 (setq untrusted nil)
399 (setq trusted nil)))
400 (cond
401 (untrusted
402 ;; The site was explicity marked as untrusted by the user
403 nil)
404 ((or (eq url-privacy-level 'paranoid)
405 (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
406 ;; user never wants cookies
407 nil)
408 ((and url-cookie-confirmation
409 (not trusted)
410 (save-window-excursion
411 (with-output-to-temp-buffer "*Cookie Warning*"
412 (mapcar
413 (function
414 (lambda (x)
415 (princ (format "%s - %s" (car x) (cdr x))))) rest))
416 (prog1
417 (not (funcall url-confirmation-func
418 (format "Allow %s to set these cookies? "
419 (url-host url-current-object))))
420 (if (get-buffer "*Cookie Warning*")
421 (kill-buffer "*Cookie Warning*")))))
422 ;; user wants to be asked, and declined.
423 nil)
424 ((url-cookie-host-can-set-p (url-host url-current-object) domain)
425 ;; Cookie is accepted by the user, and passes our security checks
426 (let ((cur nil))
427 (while rest
428 (setq cur (pop rest))
429 (url-cookie-store (car cur) (cdr cur)
430 expires domain localpart secure))))
431 (t
432 (message "%s tried to set a cookie for domain %s - rejected."
433 (url-host url-current-object) domain)))))
434
435 (defvar url-cookie-timer nil)
436
437 (defcustom url-cookie-save-interval 3600
438 "The number of seconds between automatic saves of cookies.
439 Default is 1 hour. Note that if you change this variable outside of
440 the `customize' interface after `url-do-setup' has been run, you need
441 to run the `url-cookie-setup-save-timer' function manually."
442 :set #'(lambda (var val)
443 (set-default var val)
444 (if (bound-and-true-p url-setup-done)
445 (url-cookie-setup-save-timer)))
446 :type 'integer
447 :group 'url-cookie)
448
449 (defun url-cookie-setup-save-timer ()
450 "Reset the cookie saver timer."
451 (interactive)
452 (ignore-errors (cancel-timer url-cookie-timer))
453 (setq url-cookie-timer nil)
454 (if url-cookie-save-interval
455 (setq url-cookie-timer (run-at-time url-cookie-save-interval
456 url-cookie-save-interval
457 #'url-cookie-write-file))))
458
459 (provide 'url-cookie)
460
461 ;; arch-tag: 2568751b-6452-4398-aa2d-303edadb54d7
462 ;;; url-cookie.el ends here