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