1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996-2011 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."
45 (defvar netrc-services-file
"/etc/services"
46 "The name of the services file.")
48 (defvar netrc-cache nil
)
50 (defun netrc-parse (&optional file
)
51 (interactive "fFile to Parse: ")
52 "Parse FILE and return a list of all entries in the file."
54 (setq file netrc-file
))
56 ;; We got already parsed contents; just return it.
58 (when (file-exists-p file
)
60 (let ((tokens '("machine" "default" "login"
61 "password" "account" "macdef" "force"
63 alist elem result pair
)
65 (equal (car netrc-cache
) (nth 5 (file-attributes file
))))
66 ;; Store the contents of the file heavily encrypted in memory.
67 (insert (base64-decode-string (rot13-string (cdr netrc-cache
))))
68 (insert-file-contents file
)
69 (when (string-match "\\.gpg\\'" file
)
70 (setq netrc-cache
(cons (nth 5 (file-attributes file
))
74 (goto-char (point-min))
75 ;; Go through the file, line by line.
77 (narrow-to-region (point) (point-at-eol))
78 ;; For each line, get the tokens and values.
80 (skip-chars-forward "\t ")
81 ;; Skip lines that begin with a "#".
82 (if (eq (char-after) ?
#)
83 (goto-char (point-max))
86 (if (= (following-char) ?
\")
87 (read (current-buffer))
89 (point) (progn (skip-chars-forward "^\t ")
92 ((equal elem
"macdef")
93 ;; We skip past the macro definition.
95 (while (and (zerop (forward-line 1))
97 (narrow-to-region (point) (point)))
99 ;; Tokens that don't have a following value are ignored,
101 (when (and pair
(or (cdr pair
)
102 (equal (car pair
) "default")))
104 (setq pair
(list elem
)))
106 ;; Values that haven't got a preceding token are ignored.
110 (setq pair nil
)))))))
112 (push (nreverse alist
) result
))
117 (nreverse result
))))))
119 (defun netrc-machine (list machine
&optional port defaultport
)
120 "Return the netrc values from LIST for MACHINE or for the default entry.
121 If PORT specified, only return entries with matching port tokens.
122 Entries without port tokens default to DEFAULTPORT."
126 (when (equal (cdr (assoc "machine" (car list
))) machine
)
127 (push (car list
) result
))
130 ;; No machine name matches, so we look for default entries.
132 (when (assoc "default" (car rest
))
133 (let ((elem (car rest
)))
134 (setq elem
(delete (assoc "default" elem
) elem
))
138 (setq result
(nreverse result
))
142 (not (netrc-port-equal
143 (or port defaultport
"nntp")
144 ;; when port is not given in the netrc file,
145 ;; it should mean "any port"
146 (or (netrc-get (car result
) "port")
151 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults
)
152 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
153 Matches a machine from MACHINES and a port from PORTS, giving
154 default ports DEFAULTS to `netrc-machine'.
156 MODE can be \"login\" or \"password\", suitable for passing to
158 (let ((authinfo-list (if (stringp authinfo-file-or-list
)
159 (netrc-parse authinfo-file-or-list
)
160 authinfo-file-or-list
))
161 (ports (or ports
'(nil)))
162 (defaults (or defaults
'(nil)))
167 (lambda (mode-element)
168 (netrc-machine-user-or-password
175 (dolist (machine machines
)
176 (dolist (default defaults
)
178 (let ((alist (netrc-machine authinfo-list machine port default
)))
179 (setq info
(or (netrc-get alist mode
) info
)))))))
182 (defun netrc-get (alist type
)
183 "Return the value of token TYPE from ALIST."
184 (cdr (assoc type alist
)))
186 (defun netrc-port-equal (port1 port2
)
187 (when (numberp port1
)
188 (setq port1
(or (netrc-find-service-name port1
) port1
)))
189 (when (numberp port2
)
190 (setq port2
(or (netrc-find-service-name port2
) port2
)))
193 (defun netrc-parse-services ()
194 (when (file-exists-p netrc-services-file
)
195 (let ((services nil
))
197 (insert-file-contents netrc-services-file
)
198 (while (search-forward "#" nil t
)
199 (delete-region (1- (point)) (point-at-eol)))
200 (goto-char (point-min))
201 (while (re-search-forward
202 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t
)
203 (push (list (match-string 1) (string-to-number (match-string 2))
204 (intern (downcase (match-string 3))))
206 (nreverse services
)))))
208 (defun netrc-find-service-name (number &optional type
)
209 (let ((services (netrc-parse-services))
211 (setq type
(or type
'tcp
))
212 (while (and (setq service
(pop services
))
213 (not (and (= number
(cadr service
))
214 (eq type
(car (cddr service
)))))))
217 (defun netrc-find-service-number (name &optional type
)
218 (let ((services (netrc-parse-services))
220 (setq type
(or type
'tcp
))
221 (while (and (setq service
(pop services
))
222 (not (and (string= name
(car service
))
223 (eq type
(car (cddr service
)))))))
226 (defun netrc-store-data (file host port user password
)
228 (when (file-exists-p file
)
229 (insert-file-contents file
))
230 (goto-char (point-max))
233 (insert (format "machine %s login %s password %s port %s\n"
234 host user password port
))
235 (write-region (point-min) (point-max) file nil
'silent
)))
238 (defun netrc-credentials (machine &rest ports
)
239 "Return a user name/password pair.
240 Port specifications will be prioritised in the order they are
241 listed in the PORTS list."
242 (let ((list (netrc-parse))
245 (setq found
(netrc-machine list machine
))
248 (setq found
(netrc-machine list machine
(pop ports
)))))
250 (list (cdr (assoc "login" found
))
251 (cdr (assoc "password" found
))))))
255 ;;; netrc.el ends here