85ab774910702cffc8e953b4692e301cfc6dd5a9
[gnus] / lisp / netrc.el
1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Just the .netrc parsing functionality, abstracted so other packages
29 ;; besides Gnus can use it.
30
31 ;;; Code:
32
33 ;;;
34 ;;; .netrc and .authinfo rc parsing
35 ;;;
36
37 ;; autoload password
38 (eval-and-compile
39   (autoload 'password-read "password"))
40
41 (defgroup netrc nil
42  "Netrc configuration.")
43
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."
49   :type '(choice
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))
55   :group 'netrc)
56
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))
61   :group 'netrc)
62
63 (defvar netrc-services-file "/etc/services"
64   "The name of the services file.")
65
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
72              netrc-openssl-path)
73     (let ((buffer-file-coding-system 'binary)
74           (coding-system-for-read 'binary)
75           (coding-system-for-write 'binary)
76           (password 
77            (password-read
78             (format "OpenSSL Password for cipher %s? "
79                     netrc-encrypting-method)
80             (format "netrc-openssl-password-%s"
81                     netrc-encrypting-method))))
82       (when password
83         (with-temp-buffer
84           (insert-file-contents plain-file)
85           (setenv "NETRC_OPENSSL_PASSWORD" password)
86           (shell-command-on-region 
87            (point-min) 
88            (point-max)
89            (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -e"
90                    netrc-openssl-path
91                    netrc-encrypting-method)
92            t
93            t)
94           (write-file encrypted-file t))))))
95
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)
100     (with-temp-buffer
101       (let ((tokens '("machine" "default" "login"
102                       "password" "account" "macdef" "force"
103                       "port"))
104             alist elem result pair)
105         (if (and netrc-encrypting-method
106                  netrc-openssl-path)
107             (let ((buffer-file-coding-system 'binary)
108                   (coding-system-for-read 'binary)
109                   (coding-system-for-write 'binary)
110                   (password 
111                    (password-read
112                     (format "OpenSSL Password for cipher %s? "
113                             netrc-encrypting-method)
114                     (format "netrc-openssl-password-%s" 
115                             netrc-encrypting-method))))
116               (when password
117                 (insert-file-contents file)
118                 (setenv "NETRC_OPENSSL_PASSWORD" password)
119                 (shell-command-on-region
120                  (point-min) 
121                  (point-max)
122                  (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -d"
123                          netrc-openssl-path
124                          netrc-encrypting-method)
125                  t
126                  t)))
127           (insert-file-contents file))
128         (goto-char (point-min))
129         ;; Go through the file, line by line.
130         (while (not (eobp))
131           (narrow-to-region (point) (point-at-eol))
132           ;; For each line, get the tokens and values.
133           (while (not (eobp))
134             (skip-chars-forward "\t ")
135             ;; Skip lines that begin with a "#".
136             (if (eq (char-after) ?#)
137                 (goto-char (point-max))
138               (unless (eobp)
139                 (setq elem
140                       (if (= (following-char) ?\")
141                           (read (current-buffer))
142                         (buffer-substring
143                          (point) (progn (skip-chars-forward "^\t ")
144                                         (point)))))
145                 (cond
146                  ((equal elem "macdef")
147                   ;; We skip past the macro definition.
148                   (widen)
149                   (while (and (zerop (forward-line 1))
150                               (looking-at "$")))
151                   (narrow-to-region (point) (point)))
152                  ((member elem tokens)
153                   ;; Tokens that don't have a following value are ignored,
154                   ;; except "default".
155                   (when (and pair (or (cdr pair)
156                                       (equal (car pair) "default")))
157                     (push pair alist))
158                   (setq pair (list elem)))
159                  (t
160                   ;; Values that haven't got a preceding token are ignored.
161                   (when pair
162                     (setcdr pair elem)
163                     (push pair alist)
164                     (setq pair nil)))))))
165           (when alist
166             (push (nreverse alist) result))
167           (setq alist nil
168                 pair nil)
169           (widen)
170           (forward-line 1))
171         (nreverse result)))))
172
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."
177   (let ((rest list)
178         result)
179     (while list
180       (when (equal (cdr (assoc "machine" (car list))) machine)
181         (push (car list) result))
182       (pop list))
183     (unless result
184       ;; No machine name matches, so we look for default entries.
185       (while rest
186         (when (assoc "default" (car rest))
187           (push (car rest) result))
188         (pop rest)))
189     (when result
190       (setq result (nreverse result))
191       (while (and result
192                   (not (netrc-port-equal
193                         (or port defaultport "nntp")
194                         (or (netrc-get (car result) "port")
195                             defaultport "nntp"))))
196         (pop result))
197       (car result))))
198
199 (defun netrc-get (alist type)
200   "Return the value of token TYPE from ALIST."
201   (cdr (assoc type alist)))
202
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)))
208   (equal port1 port2))
209
210 (defun netrc-parse-services ()
211   (when (file-exists-p netrc-services-file)
212     (let ((services nil))
213       (with-temp-buffer
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))))
222                 services))
223         (nreverse services)))))
224
225 (defun netrc-find-service-name (number &optional type)
226   (let ((services (netrc-parse-services))
227         service)
228     (setq type (or type 'tcp))
229     (while (and (setq service (pop services))
230                 (not (and (= number (cadr service))
231                           (eq type (caddr service)))))
232       )
233     (car service)))
234
235 (defun netrc-find-service-number (name &optional type)
236   (let ((services (netrc-parse-services))
237         service)
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)))))
242       )
243     (cadr service)))
244
245 (provide 'netrc)
246
247 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
248 ;;; netrc.el ends here