1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Just the .netrc parsing functionality, abstracted so other packages
29 ;; besides Gnus can use it.
34 ;;; .netrc and .authinfo rc parsing
39 (autoload 'password-read "password"))
42 "Netrc configuration.")
44 (defcustom netrc-encrypting-method nil
45 "Decoding method used for the netrc file.
46 Use the OpenSSL symmetric ciphers here. Leave nil for no
47 decoding. Encrypt the file with netrc-encrypt, but make sure you
48 have set netrc-encrypting-method to a non-nil value."
50 (const :tag "DES-3" "des3")
51 (const :tag "IDEA" "idea")
52 (const :tag "RC4" "rc4")
53 (string :tag "Explicit cipher name")
54 (const :tag "None" nil))
57 (defcustom netrc-openssl-path (executable-find "openssl")
58 "File path of the OpenSSL shell."
59 :type '(choice (file :tag "Location of openssl")
60 (const :tag "openssl is not installed" nil))
63 (defvar netrc-services-file "/etc/services"
64 "The name of the services file.")
66 (defun netrc-encrypt (plain-file encrypted-file)
67 (interactive "fPlain File: \nFEncrypted File: ")
68 "Encrypt FILE to ENCRYPTED-FILE with netrc-encrypting-method cipher."
69 (when (and (file-exists-p plain-file)
70 (stringp encrypted-file)
71 netrc-encrypting-method
73 (let ((buffer-file-coding-system 'binary)
74 (coding-system-for-read 'binary)
75 (coding-system-for-write 'binary)
78 (format "OpenSSL Password for cipher %s? "
79 netrc-encrypting-method)
80 (format "netrc-openssl-password-%s"
81 netrc-encrypting-method))))
84 (insert-file-contents plain-file)
85 (setenv "NETRC_OPENSSL_PASSWORD" password)
86 (shell-command-on-region
89 (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -e"
91 netrc-encrypting-method)
94 (write-file encrypted-file t))))))
96 (defun netrc-parse (file)
97 (interactive "fFile to Parse: ")
98 "Parse FILE and return an list of all entries in the file."
99 (when (file-exists-p file)
101 (let ((tokens '("machine" "default" "login"
102 "password" "account" "macdef" "force"
104 alist elem result pair)
105 (if (and netrc-encrypting-method
107 (let ((buffer-file-coding-system 'binary)
108 (coding-system-for-read 'binary)
109 (coding-system-for-write 'binary)
112 (format "OpenSSL Password for cipher %s? "
113 netrc-encrypting-method)
114 (format "netrc-openssl-password-%s"
115 netrc-encrypting-method))))
117 (insert-file-contents file)
118 (setenv "NETRC_OPENSSL_PASSWORD" password)
119 (shell-command-on-region
122 (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -d"
124 netrc-encrypting-method)
127 (insert-file-contents file))
128 (goto-char (point-min))
129 ;; Go through the file, line by line.
131 (narrow-to-region (point) (point-at-eol))
132 ;; For each line, get the tokens and values.
134 (skip-chars-forward "\t ")
135 ;; Skip lines that begin with a "#".
136 (if (eq (char-after) ?#)
137 (goto-char (point-max))
140 (if (= (following-char) ?\")
141 (read (current-buffer))
143 (point) (progn (skip-chars-forward "^\t ")
146 ((equal elem "macdef")
147 ;; We skip past the macro definition.
149 (while (and (zerop (forward-line 1))
151 (narrow-to-region (point) (point)))
152 ((member elem tokens)
153 ;; Tokens that don't have a following value are ignored,
155 (when (and pair (or (cdr pair)
156 (equal (car pair) "default")))
158 (setq pair (list elem)))
160 ;; Values that haven't got a preceding token are ignored.
164 (setq pair nil)))))))
166 (push (nreverse alist) result))
171 (nreverse result)))))
173 (defun netrc-machine (list machine &optional port defaultport)
174 "Return the netrc values from LIST for MACHINE or for the default entry.
175 If PORT specified, only return entries with matching port tokens.
176 Entries without port tokens default to DEFAULTPORT."
180 (when (equal (cdr (assoc "machine" (car list))) machine)
181 (push (car list) result))
184 ;; No machine name matches, so we look for default entries.
186 (when (assoc "default" (car rest))
187 (push (car rest) result))
190 (setq result (nreverse result))
192 (not (netrc-port-equal
193 (or port defaultport "nntp")
194 (or (netrc-get (car result) "port")
195 defaultport "nntp"))))
199 (defun netrc-get (alist type)
200 "Return the value of token TYPE from ALIST."
201 (cdr (assoc type alist)))
203 (defun netrc-port-equal (port1 port2)
204 (when (numberp port1)
205 (setq port1 (or (netrc-find-service-name port1) port1)))
206 (when (numberp port2)
207 (setq port2 (or (netrc-find-service-name port2) port2)))
210 (defun netrc-parse-services ()
211 (when (file-exists-p netrc-services-file)
212 (let ((services nil))
214 (insert-file-contents netrc-services-file)
215 (while (search-forward "#" nil t)
216 (delete-region (1- (point)) (line-end-position)))
217 (goto-char (point-min))
218 (while (re-search-forward
219 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
220 (push (list (match-string 1) (string-to-number (match-string 2))
221 (intern (downcase (match-string 3))))
223 (nreverse services)))))
225 (defun netrc-find-service-name (number &optional type)
226 (let ((services (netrc-parse-services))
228 (setq type (or type 'tcp))
229 (while (and (setq service (pop services))
230 (not (and (= number (cadr service))
231 (eq type (caddr service)))))
235 (defun netrc-find-service-number (name &optional type)
236 (let ((services (netrc-parse-services))
238 (setq type (or type 'tcp))
239 (while (and (setq service (pop services))
240 (not (and (string= name (car service))
241 (eq type (caddr service)))))
247 ;;; netrc.el ends here