netrc.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; netrc.el --- .netrc parsing functionality
  2. ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;;
  6. ;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
  7. ;; when it was part of Gnus.
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Just the .netrc parsing functionality, abstracted so other packages
  21. ;; besides Gnus can use it.
  22. ;;; Code:
  23. ;;;
  24. ;;; .netrc and .authinfo rc parsing
  25. ;;;
  26. (defgroup netrc nil
  27. "Netrc configuration."
  28. :group 'comm)
  29. (defcustom netrc-file "~/.authinfo"
  30. "File where user credentials are stored."
  31. :version "24.1"
  32. :type 'file
  33. :group 'netrc)
  34. (defvar netrc-services-file "/etc/services"
  35. "The name of the services file.")
  36. (defvar netrc-cache nil)
  37. (defun netrc-parse (&optional file)
  38. (interactive "fFile to Parse: ")
  39. "Parse FILE and return a list of all entries in the file."
  40. (unless file
  41. (setq file netrc-file))
  42. (if (listp file)
  43. ;; We got already parsed contents; just return it.
  44. file
  45. (when (file-exists-p file)
  46. (with-temp-buffer
  47. (let ((tokens '("machine" "default" "login"
  48. "password" "account" "macdef" "force"
  49. "port"))
  50. alist elem result pair)
  51. (if (and netrc-cache
  52. (equal (car netrc-cache) (nth 5 (file-attributes file))))
  53. (insert (base64-decode-string (rot13-string (cdr netrc-cache))))
  54. (insert-file-contents file)
  55. (when (string-match "\\.gpg\\'" file)
  56. ;; Store the contents of the file heavily encrypted in memory.
  57. (setq netrc-cache (cons (nth 5 (file-attributes file))
  58. (rot13-string
  59. (base64-encode-string
  60. (buffer-string)))))))
  61. (goto-char (point-min))
  62. ;; Go through the file, line by line.
  63. (while (not (eobp))
  64. (narrow-to-region (point) (point-at-eol))
  65. ;; For each line, get the tokens and values.
  66. (while (not (eobp))
  67. (skip-chars-forward "\t ")
  68. ;; Skip lines that begin with a "#".
  69. (if (eq (char-after) ?#)
  70. (goto-char (point-max))
  71. (unless (eobp)
  72. (setq elem
  73. (if (= (following-char) ?\")
  74. (read (current-buffer))
  75. (buffer-substring
  76. (point) (progn (skip-chars-forward "^\t ")
  77. (point)))))
  78. (cond
  79. ((equal elem "macdef")
  80. ;; We skip past the macro definition.
  81. (widen)
  82. (while (and (zerop (forward-line 1))
  83. (looking-at "$")))
  84. (narrow-to-region (point) (point)))
  85. ((member elem tokens)
  86. ;; Tokens that don't have a following value are ignored,
  87. ;; except "default".
  88. (when (and pair (or (cdr pair)
  89. (equal (car pair) "default")))
  90. (push pair alist))
  91. (setq pair (list elem)))
  92. (t
  93. ;; Values that haven't got a preceding token are ignored.
  94. (when pair
  95. (setcdr pair elem)
  96. (push pair alist)
  97. (setq pair nil)))))))
  98. (when alist
  99. (push (nreverse alist) result))
  100. (setq alist nil
  101. pair nil)
  102. (widen)
  103. (forward-line 1))
  104. (nreverse result))))))
  105. (defun netrc-machine (list machine &optional port defaultport)
  106. "Return the netrc values from LIST for MACHINE or for the default entry.
  107. If PORT specified, only return entries with matching port tokens.
  108. Entries without port tokens default to DEFAULTPORT."
  109. (let ((rest list)
  110. result)
  111. (while list
  112. (when (equal (cdr (assoc "machine" (car list))) machine)
  113. (push (car list) result))
  114. (pop list))
  115. (unless result
  116. ;; No machine name matches, so we look for default entries.
  117. (while rest
  118. (when (assoc "default" (car rest))
  119. (let ((elem (car rest)))
  120. (setq elem (delete (assoc "default" elem) elem))
  121. (push elem result)))
  122. (pop rest)))
  123. (when result
  124. (setq result (nreverse result))
  125. (if (not port)
  126. (car result)
  127. (while (and result
  128. (not (netrc-port-equal
  129. (or port defaultport "nntp")
  130. ;; when port is not given in the netrc file,
  131. ;; it should mean "any port"
  132. (or (netrc-get (car result) "port")
  133. defaultport port))))
  134. (pop result))
  135. (car result)))))
  136. (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
  137. "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
  138. Matches a machine from MACHINES and a port from PORTS, giving
  139. default ports DEFAULTS to `netrc-machine'.
  140. MODE can be \"login\" or \"password\", suitable for passing to
  141. `netrc-get'."
  142. (let ((authinfo-list (if (stringp authinfo-file-or-list)
  143. (netrc-parse authinfo-file-or-list)
  144. authinfo-file-or-list))
  145. (ports (or ports '(nil)))
  146. (defaults (or defaults '(nil)))
  147. info)
  148. (if (listp mode)
  149. (setq info
  150. (mapcar
  151. (lambda (mode-element)
  152. (netrc-machine-user-or-password
  153. mode-element
  154. authinfo-list
  155. machines
  156. ports
  157. defaults))
  158. mode))
  159. (dolist (machine machines)
  160. (dolist (default defaults)
  161. (dolist (port ports)
  162. (let ((alist (netrc-machine authinfo-list machine port default)))
  163. (setq info (or (netrc-get alist mode) info)))))))
  164. info))
  165. (defun netrc-get (alist type)
  166. "Return the value of token TYPE from ALIST."
  167. (cdr (assoc type alist)))
  168. (defun netrc-port-equal (port1 port2)
  169. (when (numberp port1)
  170. (setq port1 (or (netrc-find-service-name port1) port1)))
  171. (when (numberp port2)
  172. (setq port2 (or (netrc-find-service-name port2) port2)))
  173. (equal port1 port2))
  174. (defun netrc-parse-services ()
  175. (when (file-exists-p netrc-services-file)
  176. (let ((services nil))
  177. (with-temp-buffer
  178. (insert-file-contents netrc-services-file)
  179. (while (search-forward "#" nil t)
  180. (delete-region (1- (point)) (point-at-eol)))
  181. (goto-char (point-min))
  182. (while (re-search-forward
  183. "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
  184. (push (list (match-string 1) (string-to-number (match-string 2))
  185. (intern (downcase (match-string 3))))
  186. services))
  187. (nreverse services)))))
  188. (defun netrc-find-service-name (number &optional type)
  189. (let ((services (netrc-parse-services))
  190. service)
  191. (setq type (or type 'tcp))
  192. (while (and (setq service (pop services))
  193. (not (and (= number (cadr service))
  194. (eq type (car (cddr service)))))))
  195. (car service)))
  196. (defun netrc-find-service-number (name &optional type)
  197. (let ((services (netrc-parse-services))
  198. service)
  199. (setq type (or type 'tcp))
  200. (while (and (setq service (pop services))
  201. (not (and (string= name (car service))
  202. (eq type (car (cddr service)))))))
  203. (cadr service)))
  204. (defun netrc-store-data (file host port user password)
  205. (with-temp-buffer
  206. (when (file-exists-p file)
  207. (insert-file-contents file))
  208. (goto-char (point-max))
  209. (unless (bolp)
  210. (insert "\n"))
  211. (insert (format "machine %s login %s password %s port %s\n"
  212. host user password port))
  213. (write-region (point-min) (point-max) file nil 'silent)))
  214. ;;;###autoload
  215. (defun netrc-credentials (machine &rest ports)
  216. "Return a user name/password pair.
  217. Port specifications will be prioritized in the order they are
  218. listed in the PORTS list."
  219. (let ((list (netrc-parse))
  220. found)
  221. (if (not ports)
  222. (setq found (netrc-machine list machine))
  223. (while (and ports
  224. (not found))
  225. (setq found (netrc-machine list machine (pop ports)))))
  226. (when found
  227. (list (cdr (assoc "login" found))
  228. (cdr (assoc "password" found))))))
  229. (provide 'netrc)
  230. ;;; netrc.el ends here