1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8 ;; when it was part of Gnus.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Just the .netrc parsing functionality, abstracted so other packages
28 ;; besides Gnus can use it.
33 ;;; .netrc and .authinfo rc parsing
37 "Netrc configuration."
40 (defcustom netrc-file
"~/.authinfo"
41 "File where user credentials are stored."
46 (defvar netrc-services-file
"/etc/services"
47 "The name of the services file.")
49 (defvar netrc-cache nil
)
51 (defun netrc-parse (&optional file
)
52 (interactive "fFile to Parse: ")
53 "Parse FILE and return a list of all entries in the file."
55 (setq file netrc-file
))
57 ;; We got already parsed contents; just return it.
59 (when (file-exists-p file
)
61 (let ((tokens '("machine" "default" "login"
62 "password" "account" "macdef" "force"
64 alist elem result pair
)
66 (equal (car netrc-cache
) (nth 5 (file-attributes file
))))
67 (insert (base64-decode-string (rot13-string (cdr netrc-cache
))))
68 (insert-file-contents file
)
69 (when (string-match "\\.gpg\\'" file
)
70 ;; Store the contents of the file heavily encrypted in memory.
71 (setq netrc-cache
(cons (nth 5 (file-attributes file
))
75 (goto-char (point-min))
76 ;; Go through the file, line by line.
78 (narrow-to-region (point) (point-at-eol))
79 ;; For each line, get the tokens and values.
81 (skip-chars-forward "\t ")
82 ;; Skip lines that begin with a "#".
83 (if (eq (char-after) ?
#)
84 (goto-char (point-max))
87 (if (= (following-char) ?
\")
88 (read (current-buffer))
90 (point) (progn (skip-chars-forward "^\t ")
93 ((equal elem
"macdef")
94 ;; We skip past the macro definition.
96 (while (and (zerop (forward-line 1))
98 (narrow-to-region (point) (point)))
100 ;; Tokens that don't have a following value are ignored,
102 (when (and pair
(or (cdr pair
)
103 (equal (car pair
) "default")))
105 (setq pair
(list elem
)))
107 ;; Values that haven't got a preceding token are ignored.
111 (setq pair nil
)))))))
113 (push (nreverse alist
) result
))
118 (nreverse result
))))))
120 (defun netrc-machine (list machine
&optional port defaultport
)
121 "Return the netrc values from LIST for MACHINE or for the default entry.
122 If PORT specified, only return entries with matching port tokens.
123 Entries without port tokens default to DEFAULTPORT."
127 (when (equal (cdr (assoc "machine" (car list
))) machine
)
128 (push (car list
) result
))
131 ;; No machine name matches, so we look for default entries.
133 (when (assoc "default" (car rest
))
134 (let ((elem (car rest
)))
135 (setq elem
(delete (assoc "default" elem
) elem
))
139 (setq result
(nreverse result
))
143 (not (netrc-port-equal
144 (or port defaultport
"nntp")
145 ;; when port is not given in the netrc file,
146 ;; it should mean "any port"
147 (or (netrc-get (car result
) "port")
152 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults
)
153 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
154 Matches a machine from MACHINES and a port from PORTS, giving
155 default ports DEFAULTS to `netrc-machine'.
157 MODE can be \"login\" or \"password\", suitable for passing to
159 (let ((authinfo-list (if (stringp authinfo-file-or-list
)
160 (netrc-parse authinfo-file-or-list
)
161 authinfo-file-or-list
))
162 (ports (or ports
'(nil)))
163 (defaults (or defaults
'(nil)))
168 (lambda (mode-element)
169 (netrc-machine-user-or-password
176 (dolist (machine machines
)
177 (dolist (default defaults
)
179 (let ((alist (netrc-machine authinfo-list machine port default
)))
180 (setq info
(or (netrc-get alist mode
) info
)))))))
183 (defun netrc-get (alist type
)
184 "Return the value of token TYPE from ALIST."
185 (cdr (assoc type alist
)))
187 (defun netrc-port-equal (port1 port2
)
188 (when (numberp port1
)
189 (setq port1
(or (netrc-find-service-name port1
) port1
)))
190 (when (numberp port2
)
191 (setq port2
(or (netrc-find-service-name port2
) port2
)))
194 (defun netrc-parse-services ()
195 (when (file-exists-p netrc-services-file
)
196 (let ((services nil
))
198 (insert-file-contents netrc-services-file
)
199 (while (search-forward "#" nil t
)
200 (delete-region (1- (point)) (point-at-eol)))
201 (goto-char (point-min))
202 (while (re-search-forward
203 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t
)
204 (push (list (match-string 1) (string-to-number (match-string 2))
205 (intern (downcase (match-string 3))))
207 (nreverse services
)))))
209 (defun netrc-find-service-name (number &optional type
)
210 (let ((services (netrc-parse-services))
212 (setq type
(or type
'tcp
))
213 (while (and (setq service
(pop services
))
214 (not (and (= number
(cadr service
))
215 (eq type
(car (cddr service
)))))))
219 (defun netrc-credentials (machine &rest ports
)
220 "Return a user name/password pair.
221 Port specifications will be prioritized in the order they are
222 listed in the PORTS list."
223 (let ((list (netrc-parse))
226 (setq found
(netrc-machine list machine
))
229 (setq found
(netrc-machine list machine
(pop ports
)))))
231 (list (cdr (assoc "login" found
))
232 (cdr (assoc "password" found
))))))
236 ;;; netrc.el ends here