1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This is the auth-source.el package. It lets users tell Gnus how to
28 ;; authenticate in a single place. Simplicity is the goal. Instead
29 ;; of providing 5000 options, we'll stick to simple, easy to
30 ;; understand options.
33 ;; (require 'auth-source)
34 ;; (customize-variable 'auth-sources) ;; optional
36 ;; now, whatever sources you've defined for password have to be available
38 ;; if you want encrypted sources, which is strongly recommended, do
39 ;; (require 'epa-file)
42 ;; before you put some data in ~/.authinfo.gpg (the default place)
46 (eval-when-compile (require 'cl))
47 (eval-when-compile (require 'netrc))
49 (defgroup auth-source nil
50 "Authentication sources."
51 :version "23.1" ;; No Gnus
54 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
55 (pop3 "pop3" "pop" "pop3s" "110" "995")
59 "List of authentication protocols and their names"
62 :version "23.1" ;; No Gnus
63 :type '(repeat :tag "Authentication Protocols"
64 (cons :tag "Protocol Entry"
65 (symbol :tag "Protocol")
67 (string :tag "Name")))))
69 ;;; generate all the protocols in a format Customize can use
70 (defconst auth-source-protocols-customize
72 (let ((p (car-safe a)))
74 :tag (upcase (symbol-name p))
76 auth-source-protocols))
78 ;;; this default will be changed to ~/.authinfo.gpg
79 (defcustom auth-sources '((:source "~/.authinfo.enc" :host t :protocol t))
80 "List of authentication sources.
82 Each entry is the authentication type with optional properties."
84 :version "23.1" ;; No Gnus
85 :type `(repeat :tag "Authentication Sources"
86 (list :tag "Source definition"
87 (const :format "" :value :source)
88 (string :tag "Authentication Source")
89 (const :format "" :value :server)
90 (choice :tag "Server (logical name) choice"
92 (regexp :tag "Server regular expression (TODO)")
93 (const :tag "Fallback" nil))
94 (const :format "" :value :host)
95 (choice :tag "Host (machine) choice"
97 (regexp :tag "Host (machine) regular expression (TODO)")
98 (const :tag "Fallback" nil))
99 (const :format "" :value :protocol)
100 (choice :tag "Protocol"
102 (const :tag "Fallback" nil)
103 ,@auth-source-protocols-customize))))
105 ;; temp for debugging
106 ;; (unintern 'auth-source-protocols)
107 ;; (unintern 'auth-sources)
108 ;; (customize-variable 'auth-sources)
109 ;; (setq auth-sources nil)
110 ;; (format "%S" auth-sources)
111 ;; (customize-variable 'auth-source-protocols)
112 ;; (setq auth-source-protocols nil)
113 ;; (format "%S" auth-source-protocols)
114 ;; (auth-source-pick "a" 'imap)
115 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
116 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
117 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
118 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
119 ;; (auth-source-protocol-defaults 'imap)
121 (defun auth-source-pick (server host protocol &optional fallback)
122 "Parse `auth-sources' for SERVER, HOST, and PROTOCOL matches.
124 Returns fallback choices (where SERVER. PROTOCOL or HOST are nil) with FALLBACK t."
125 (interactive "sHost: \nsProtocol: \n") ;for testing
127 (dolist (choice auth-sources)
128 (let ((s (plist-get choice :server))
129 (h (plist-get choice :host))
130 (p (plist-get choice :protocol)))
133 (and (stringp s) (string-match s server))
134 (and fallback (equal s nil)))
136 (and (stringp h) (string-match h host))
137 (and fallback (equal h nil)))
139 (and (symbolp p) (equal p protocol))
140 (and fallback (equal p nil))))
141 (push choice choices))))
145 (auth-source-pick server host protocol t)))))
147 (defun auth-source-user-or-password (mode server host protocol)
148 "Find user or password (from the string MODE) matching SERVER, HOST, and PROTOCOL."
150 (dolist (choice (auth-source-pick server host protocol))
151 (setq found (netrc-machine-user-or-password
153 (plist-get choice :source)
155 (list (format "%s" protocol))
156 (auth-source-protocol-defaults protocol)))
160 (defun auth-source-protocol-defaults (protocol)
161 "Return a list of default ports and names for PROTOCOL."
162 (cdr-safe (assoc protocol auth-source-protocols)))
164 (defun auth-source-user-or-password-imap (mode server host)
165 (auth-source-user-or-password mode server host 'imap))
167 (defun auth-source-user-or-password-pop3 (mode server host)
168 (auth-source-user-or-password mode server host 'pop3))
170 (defun auth-source-user-or-password-ssh (mode server host)
171 (auth-source-user-or-password mode server host 'ssh))
173 (defun auth-source-user-or-password-sftp (mode server host)
174 (auth-source-user-or-password mode server host 'sftp))
176 (defun auth-source-user-or-password-smtp (mode server host)
177 (auth-source-user-or-password mode server host 'smtp))
179 (provide 'auth-source)
181 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
182 ;;; auth-source.el ends here