url-cookie.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. ;;; url-cookie.el --- URL cookie support
  2. ;; Copyright (C) 1996-1999, 2004-2015 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'url-util)
  19. (require 'url-parse)
  20. (require 'url-domsuf)
  21. (eval-when-compile (require 'cl-lib))
  22. (defgroup url-cookie nil
  23. "URL cookies."
  24. :prefix "url-"
  25. :prefix "url-cookie-"
  26. :group 'url)
  27. ;; A cookie is stored internally as a vector of 7 slots
  28. ;; [ url-cookie NAME VALUE EXPIRES LOCALPART DOMAIN SECURE ]
  29. (cl-defstruct (url-cookie
  30. (:constructor url-cookie-create)
  31. (:copier nil)
  32. (:type vector)
  33. :named)
  34. name value expires localpart domain secure)
  35. (defvar url-cookie-storage nil "Where cookies are stored.")
  36. (defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
  37. (defcustom url-cookie-file nil
  38. "File where cookies are stored on disk."
  39. :type '(choice (const :tag "Default" :value nil) file)
  40. :group 'url-file
  41. :group 'url-cookie)
  42. (defcustom url-cookie-confirmation nil
  43. "If non-nil, confirmation by the user is required to accept HTTP cookies."
  44. :type 'boolean
  45. :group 'url-cookie)
  46. (defcustom url-cookie-multiple-line nil
  47. "If nil, HTTP requests put all cookies for the server on one line.
  48. Some web servers, such as http://www.hotmail.com/, only accept cookies
  49. when they are on one line. This is broken behavior, but just try
  50. telling Microsoft that."
  51. :type 'boolean
  52. :group 'url-cookie)
  53. (defvar url-cookies-changed-since-last-save nil
  54. "Whether the cookies list has changed since the last save operation.")
  55. (defun url-cookie-parse-file (&optional fname)
  56. "Load FNAME, default `url-cookie-file'."
  57. ;; It's completely normal for the cookies file not to exist yet.
  58. (load (or fname url-cookie-file) t t))
  59. (defun url-cookie-clean-up (&optional secure)
  60. (let ((var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
  61. new new-cookies)
  62. (dolist (cur (symbol-value var))
  63. (setq new-cookies nil)
  64. (dolist (cur-cookie (cdr cur))
  65. (or (not (url-cookie-p cur-cookie))
  66. (url-cookie-expired-p cur-cookie)
  67. (null (url-cookie-expires cur-cookie))
  68. (setq new-cookies (cons cur-cookie new-cookies))))
  69. (when new-cookies
  70. (setcdr cur new-cookies)
  71. (setq new (cons cur new))))
  72. (set var new)))
  73. (defun url-cookie-write-file (&optional fname)
  74. (when url-cookies-changed-since-last-save
  75. (or fname (setq fname (expand-file-name url-cookie-file)))
  76. (if (condition-case nil
  77. (progn
  78. (url-make-private-file fname)
  79. nil)
  80. (error t))
  81. (message "Error accessing cookie file `%s'" fname)
  82. (url-cookie-clean-up)
  83. (url-cookie-clean-up t)
  84. (with-temp-buffer
  85. (insert ";; Emacs-W3 HTTP cookies file\n"
  86. ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
  87. "(setq url-cookie-storage\n '")
  88. (let ((print-length nil) (print-level nil))
  89. (pp url-cookie-storage (current-buffer))
  90. (insert ")\n(setq url-cookie-secure-storage\n '")
  91. (pp url-cookie-secure-storage (current-buffer)))
  92. (insert ")\n")
  93. (insert " \n;; Local Variables:\n"
  94. ";; version-control: never\n"
  95. ";; no-byte-compile: t\n"
  96. ";; End:\n")
  97. (set (make-local-variable 'version-control) 'never)
  98. (write-file fname))
  99. (setq url-cookies-changed-since-last-save nil))))
  100. (defun url-cookie-store (name value &optional expires domain localpart secure)
  101. "Store a cookie."
  102. (let ((storage (if secure url-cookie-secure-storage url-cookie-storage))
  103. tmp found-domain)
  104. ;; First, look for a matching domain.
  105. (if (setq found-domain (assoc domain storage))
  106. ;; Need to either stick the new cookie in existing domain storage
  107. ;; or possibly replace an existing cookie if the names match.
  108. (unless (dolist (cur (setq storage (cdr found-domain)) tmp)
  109. (and (equal localpart (url-cookie-localpart cur))
  110. (equal name (url-cookie-name cur))
  111. (progn
  112. (setf (url-cookie-expires cur) expires)
  113. (setf (url-cookie-value cur) value)
  114. (setq tmp t))))
  115. ;; New cookie.
  116. (setcdr found-domain (cons
  117. (url-cookie-create :name name
  118. :value value
  119. :expires expires
  120. :domain domain
  121. :localpart localpart
  122. :secure secure)
  123. (cdr found-domain))))
  124. ;; Need to add a new top-level domain.
  125. (setq tmp (url-cookie-create :name name
  126. :value value
  127. :expires expires
  128. :domain domain
  129. :localpart localpart
  130. :secure secure))
  131. (cond (storage
  132. (setcdr storage (cons (list domain tmp) (cdr storage))))
  133. (secure
  134. (setq url-cookie-secure-storage (list (list domain tmp))))
  135. (t
  136. (setq url-cookie-storage (list (list domain tmp))))))))
  137. (defun url-cookie-expired-p (cookie)
  138. "Return non-nil if COOKIE is expired."
  139. (let ((exp (url-cookie-expires cookie)))
  140. (and (> (length exp) 0)
  141. (condition-case ()
  142. (> (float-time) (float-time (date-to-time exp)))
  143. (error nil)))))
  144. (defun url-cookie-retrieve (host &optional localpart secure)
  145. "Retrieve all cookies for a specified HOST and LOCALPART."
  146. (let ((storage (if secure
  147. (append url-cookie-secure-storage url-cookie-storage)
  148. url-cookie-storage))
  149. (case-fold-search t)
  150. cookies retval localpart-match)
  151. (dolist (cur storage)
  152. (setq cookies (cdr cur))
  153. (if (and (car cur)
  154. (string-match
  155. (concat "^.*"
  156. (regexp-quote
  157. ;; Remove the dot from wildcard domains
  158. ;; before matching.
  159. (if (eq ?. (aref (car cur) 0))
  160. (substring (car cur) 1)
  161. (car cur)))
  162. "$") host))
  163. ;; The domains match - a possible hit!
  164. (dolist (cur cookies)
  165. (and (if (and (stringp
  166. (setq localpart-match (url-cookie-localpart cur)))
  167. (stringp localpart))
  168. (string-match (concat "^" (regexp-quote localpart-match))
  169. localpart)
  170. (equal localpart localpart-match))
  171. (not (url-cookie-expired-p cur))
  172. (setq retval (cons cur retval))))))
  173. retval))
  174. (defun url-cookie-generate-header-lines (host localpart secure)
  175. (let ((cookies (url-cookie-retrieve host localpart secure))
  176. retval chunk)
  177. ;; Have to sort this for sending most specific cookies first.
  178. (setq cookies (and cookies
  179. (sort cookies
  180. (lambda (x y)
  181. (> (length (url-cookie-localpart x))
  182. (length (url-cookie-localpart y)))))))
  183. (dolist (cur cookies)
  184. (setq chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
  185. retval (if (and url-cookie-multiple-line
  186. (< 80 (+ (length retval) (length chunk) 4)))
  187. (concat retval "\r\nCookie: " chunk)
  188. (if retval
  189. (concat retval "; " chunk)
  190. (concat "Cookie: " chunk)))))
  191. (if retval
  192. (concat retval "\r\n")
  193. "")))
  194. (defcustom url-cookie-trusted-urls nil
  195. "A list of regular expressions matching URLs to always accept cookies from."
  196. :type '(repeat regexp)
  197. :group 'url-cookie)
  198. (defcustom url-cookie-untrusted-urls nil
  199. "A list of regular expressions matching URLs to never accept cookies from."
  200. :type '(repeat regexp)
  201. :group 'url-cookie)
  202. (defun url-cookie-host-can-set-p (host domain)
  203. (let ((last nil)
  204. (case-fold-search t))
  205. (if (string= host domain) ; Apparently netscape lets you do this
  206. t
  207. ;; Remove the dot from wildcard domains before matching.
  208. (when (eq ?. (aref domain 0))
  209. (setq domain (substring domain 1)))
  210. (and (url-domsuf-cookie-allowed-p domain)
  211. ;; Need to check and make sure the host is actually _in_ the
  212. ;; domain it wants to set a cookie for though.
  213. (string-match (concat (regexp-quote domain)
  214. "$") host)))))
  215. (defun url-cookie-handle-set-cookie (str)
  216. (setq url-cookies-changed-since-last-save t)
  217. (let* ((args (url-parse-args str t))
  218. (case-fold-search t)
  219. (secure (and (assoc-string "secure" args t) t))
  220. (domain (or (cdr-safe (assoc-string "domain" args t))
  221. (url-host url-current-object)))
  222. (current-url (url-view-url t))
  223. (trusted url-cookie-trusted-urls)
  224. (untrusted url-cookie-untrusted-urls)
  225. (expires (cdr-safe (assoc-string "expires" args t)))
  226. (localpart (or (cdr-safe (assoc-string "path" args t))
  227. (file-name-directory
  228. (url-filename url-current-object))))
  229. (rest nil))
  230. (dolist (this args)
  231. (or (member (downcase (car this)) '("secure" "domain" "expires" "path"))
  232. (setq rest (cons this rest))))
  233. ;; Sometimes we get dates that the timezone package cannot handle very
  234. ;; gracefully - take care of this here, instead of in url-cookie-expired-p
  235. ;; to speed things up.
  236. (and expires
  237. (string-match
  238. (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
  239. "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
  240. expires)
  241. (setq expires (concat (match-string 1 expires) " "
  242. (match-string 2 expires) " "
  243. (match-string 3 expires) " "
  244. (match-string 4 expires) " ["
  245. (match-string 5 expires) "]")))
  246. ;; This one is for older Emacs/XEmacs variants that don't
  247. ;; understand this format without tenths of a second in it.
  248. ;; Wednesday, 30-Dec-2037 16:00:00 GMT
  249. ;; - vs -
  250. ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
  251. (and expires
  252. (string-match
  253. "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
  254. expires)
  255. (setq expires (concat (match-string 1 expires) "-" ; day
  256. (match-string 2 expires) "-" ; month
  257. (match-string 3 expires) " " ; year
  258. (match-string 4 expires) ".00 " ; hour:minutes:seconds
  259. (match-string 6 expires)))) ":" ; timezone
  260. (while (consp trusted)
  261. (if (string-match (car trusted) current-url)
  262. (setq trusted (- (match-end 0) (match-beginning 0)))
  263. (pop trusted)))
  264. (while (consp untrusted)
  265. (if (string-match (car untrusted) current-url)
  266. (setq untrusted (- (match-end 0) (match-beginning 0)))
  267. (pop untrusted)))
  268. (and trusted untrusted
  269. ;; Choose the more specific match.
  270. (set (if (> trusted untrusted) 'untrusted 'trusted) nil))
  271. (cond
  272. (untrusted
  273. ;; The site was explicitly marked as untrusted by the user.
  274. nil)
  275. ((or (eq url-privacy-level 'paranoid)
  276. (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
  277. ;; User never wants cookies.
  278. nil)
  279. ((and url-cookie-confirmation
  280. (not trusted)
  281. (save-window-excursion
  282. (with-output-to-temp-buffer "*Cookie Warning*"
  283. (dolist (x rest)
  284. (princ (format "%s - %s" (car x) (cdr x)))))
  285. (prog1
  286. (not (funcall url-confirmation-func
  287. (format "Allow %s to set these cookies? "
  288. (url-host url-current-object))))
  289. (if (get-buffer "*Cookie Warning*")
  290. (kill-buffer "*Cookie Warning*")))))
  291. ;; User wants to be asked, and declined.
  292. nil)
  293. ((url-cookie-host-can-set-p (url-host url-current-object) domain)
  294. ;; Cookie is accepted by the user, and passes our security checks.
  295. (dolist (cur rest)
  296. (url-cookie-store (car cur) (cdr cur) expires domain localpart secure)))
  297. (t
  298. (url-lazy-message "%s tried to set a cookie for domain %s - rejected."
  299. (url-host url-current-object) domain)))))
  300. (defvar url-cookie-timer nil)
  301. (defcustom url-cookie-save-interval 3600
  302. "The number of seconds between automatic saves of cookies.
  303. Default is 1 hour. Note that if you change this variable outside of
  304. the `customize' interface after `url-do-setup' has been run, you need
  305. to run the `url-cookie-setup-save-timer' function manually."
  306. :set #'(lambda (var val)
  307. (set-default var val)
  308. (if (bound-and-true-p url-setup-done)
  309. (url-cookie-setup-save-timer)))
  310. :type 'integer
  311. :group 'url-cookie)
  312. (defun url-cookie-setup-save-timer ()
  313. "Reset the cookie saver timer."
  314. (interactive)
  315. (ignore-errors (cancel-timer url-cookie-timer))
  316. (setq url-cookie-timer nil)
  317. (if url-cookie-save-interval
  318. (setq url-cookie-timer (run-at-time url-cookie-save-interval
  319. url-cookie-save-interval
  320. #'url-cookie-write-file))))
  321. ;;; Mode for listing and editing cookies.
  322. (defun url-cookie-list ()
  323. "Display a buffer listing the current URL cookies, if there are any.
  324. Use \\<url-cookie-mode-map>\\\[url-cookie-delete] to remove cookies."
  325. (interactive)
  326. (when (and (null url-cookie-secure-storage)
  327. (null url-cookie-storage))
  328. (error "No cookies are defined"))
  329. (pop-to-buffer "*url cookies*")
  330. (let ((inhibit-read-only t)
  331. (domains (sort
  332. (copy-sequence
  333. (append url-cookie-secure-storage
  334. url-cookie-storage))
  335. (lambda (e1 e2)
  336. (string< (car e1) (car e2)))))
  337. (domain-length 0)
  338. start name format domain)
  339. (erase-buffer)
  340. (url-cookie-mode)
  341. (dolist (elem domains)
  342. (setq domain-length (max domain-length (length (car elem)))))
  343. (setq format (format "%%-%ds %%-20s %%s" domain-length)
  344. header-line-format
  345. (concat " " (format format "Domain" "Name" "Value")))
  346. (dolist (elem domains)
  347. (setq domain (car elem))
  348. (dolist (cookie (sort (copy-sequence (cdr elem))
  349. (lambda (c1 c2)
  350. (string< (url-cookie-name c1)
  351. (url-cookie-name c2)))))
  352. (setq start (point)
  353. name (url-cookie-name cookie))
  354. (when (> (length name) 20)
  355. (setq name (substring name 0 20)))
  356. (insert (format format domain name
  357. (url-cookie-value cookie))
  358. "\n")
  359. (setq domain "")
  360. (put-text-property start (1+ start) 'url-cookie cookie)))
  361. (goto-char (point-min))))
  362. (defun url-cookie-delete ()
  363. "Delete the cookie on the current line."
  364. (interactive)
  365. (let ((cookie (get-text-property (line-beginning-position) 'url-cookie))
  366. (inhibit-read-only t)
  367. variable)
  368. (unless cookie
  369. (error "No cookie on the current line"))
  370. (setq variable (if (url-cookie-secure cookie)
  371. 'url-cookie-secure-storage
  372. 'url-cookie-storage))
  373. (let* ((list (symbol-value variable))
  374. (elem (assoc (url-cookie-domain cookie) list)))
  375. (setq elem (delq cookie elem))
  376. (when (zerop (length (cdr elem)))
  377. (setq list (delq elem list)))
  378. (set variable list))
  379. (setq url-cookies-changed-since-last-save t)
  380. (url-cookie-write-file)
  381. (delete-region (line-beginning-position)
  382. (progn
  383. (forward-line 1)
  384. (point)))))
  385. (defun url-cookie-quit ()
  386. "Kill the current buffer."
  387. (interactive)
  388. (kill-buffer (current-buffer)))
  389. (defvar url-cookie-mode-map
  390. (let ((map (make-sparse-keymap)))
  391. (suppress-keymap map)
  392. (define-key map "q" 'url-cookie-quit)
  393. (define-key map [delete] 'url-cookie-delete)
  394. (define-key map [(control k)] 'url-cookie-delete)
  395. map))
  396. (define-derived-mode url-cookie-mode nil "URL Cookie"
  397. "Mode for listing cookies.
  398. \\{url-cookie-mode-map}"
  399. (buffer-disable-undo)
  400. (setq buffer-read-only t
  401. truncate-lines t))
  402. (provide 'url-cookie)
  403. ;;; url-cookie.el ends here