auth.texi 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. \input texinfo @c -*-texinfo-*-
  2. @include gnus-overrides.texi
  3. @set VERSION 0.3
  4. @setfilename ../../info/auth.info
  5. @settitle Emacs auth-source Library @value{VERSION}
  6. @include docstyle.texi
  7. @copying
  8. This file describes the Emacs auth-source library.
  9. Copyright @copyright{} 2008--2017 Free Software Foundation, Inc.
  10. @quotation
  11. Permission is granted to copy, distribute and/or modify this document
  12. under the terms of the GNU Free Documentation License, Version 1.3 or
  13. any later version published by the Free Software Foundation; with no
  14. Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
  15. and with the Back-Cover Texts as in (a) below. A copy of the license
  16. is included in the section entitled ``GNU Free Documentation License''.
  17. (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
  18. modify this GNU manual.''
  19. @end quotation
  20. @end copying
  21. @dircategory Emacs lisp libraries
  22. @direntry
  23. * Auth-source: (auth). The Emacs auth-source library.
  24. @end direntry
  25. @titlepage
  26. @ifset WEBHACKDEVEL
  27. @title Emacs auth-source Library (DEVELOPMENT VERSION)
  28. @end ifset
  29. @ifclear WEBHACKDEVEL
  30. @title Emacs auth-source Library
  31. @end ifclear
  32. @author by Ted Zlatanov
  33. @page
  34. @vskip 0pt plus 1filll
  35. @insertcopying
  36. @end titlepage
  37. @contents
  38. @ifnottex
  39. @node Top
  40. @top Emacs auth-source
  41. This manual describes the Emacs auth-source library.
  42. It is a way for multiple applications to share a single configuration
  43. (in Emacs and in files) for user convenience.
  44. @insertcopying
  45. @menu
  46. * Overview:: Overview of the auth-source library.
  47. * Help for users::
  48. * Multiple GMail accounts with Gnus::
  49. * Secret Service API::
  50. * The Unix password store::
  51. * Help for developers::
  52. * GnuPG and EasyPG Assistant Configuration::
  53. * GNU Free Documentation License:: The license for this documentation.
  54. * Index::
  55. * Function Index::
  56. * Variable Index::
  57. @end menu
  58. @end ifnottex
  59. @node Overview
  60. @chapter Overview
  61. The auth-source library is simply a way for Emacs and Gnus, among
  62. others, to answer the old burning question ``What are my user name and
  63. password?''
  64. (This is different from the old question about burning ``Where is the
  65. fire extinguisher, please?''.)
  66. The auth-source library supports more than just the user name or the
  67. password (known as the secret).
  68. Similarly, the auth-source library supports multiple storage backend,
  69. currently either the classic ``netrc'' backend, examples of which you
  70. can see later in this document, the Secret Service API, and pass, the
  71. standard unix password manager. This is done with EIEIO-based
  72. backends and you can write your own if you want.
  73. @node Help for users
  74. @chapter Help for users
  75. ``Netrc'' files are a de facto standard. They look like this:
  76. @example
  77. machine @var{mymachine} login @var{myloginname} password @var{mypassword} port @var{myport}
  78. @end example
  79. The @code{machine} is the server (either a DNS name or an IP address).
  80. It's known as @var{:host} in @code{auth-source-search} queries. You
  81. can also use @code{host}.
  82. The @code{port} is the connection port or protocol. It's known as
  83. @var{:port} in @code{auth-source-search} queries.
  84. The @code{user} is the user name. It's known as @var{:user} in
  85. @code{auth-source-search} queries. You can also use @code{login} and
  86. @code{account}.
  87. You can use spaces inside a password or other token by surrounding the
  88. token with either single or double quotes.
  89. You can use apostrophes inside a password or other token by
  90. surrounding it with double quotes, e.g., @code{"he'llo"}. Similarly you
  91. can use double quotes inside a password or other token by surrounding
  92. it with apostrophes, e.g., @code{'he"llo'}. You can't mix both (so a
  93. password or other token can't have both apostrophes and double quotes).
  94. All this is optional. You could just say (but we don't recommend it,
  95. we're just showing that it's possible)
  96. @example
  97. password @var{mypassword}
  98. @end example
  99. to use the same password everywhere. Again, @emph{DO NOT DO THIS} or
  100. you will be pwned as the kids say.
  101. ``Netrc'' files are usually called @file{.authinfo} or @file{.netrc};
  102. nowadays @file{.authinfo} seems to be more popular and the auth-source
  103. library encourages this confusion by accepting both, as you'll see
  104. later.
  105. If you have problems with the search, set @code{auth-source-debug} to
  106. @code{'trivia} and see what host, port, and user the library is
  107. checking in the @file{*Messages*} buffer. Ditto for any other
  108. problems, your first step is always to see what's being checked. The
  109. second step, of course, is to write a blog entry about it and wait for
  110. the answer in the comments.
  111. You can customize the variable @code{auth-sources}. The following may
  112. be needed if you are using an older version of Emacs or if the
  113. auth-source library is not loaded for some other reason.
  114. @lisp
  115. (require 'auth-source) ;; probably not necessary
  116. (customize-variable 'auth-sources) ;; optional, do it once
  117. @end lisp
  118. @defvar auth-sources
  119. The @code{auth-sources} variable tells the auth-source library where
  120. your netrc files, Secret Service API collection items, or your
  121. password store live for a particular host and protocol. While you can
  122. get fancy, the default and simplest configuration is:
  123. @lisp
  124. ;;; old default: required :host and :port, not needed anymore
  125. (setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
  126. ;;; mostly equivalent (see below about fallbacks) but shorter:
  127. (setq auth-sources '((:source "~/.authinfo.gpg")))
  128. ;;; even shorter and the @emph{default}:
  129. (setq auth-sources '("~/.authinfo.gpg" "~/.authinfo" "~/.netrc"))
  130. ;;; use the Secrets API @var{Login} collection
  131. ;;; (@pxref{Secret Service API})
  132. (setq auth-sources '("secrets:Login"))
  133. ;;; use pass (@file{~/.password-store})
  134. ;;; (@pxref{The Unix password store})
  135. (setq auth-sources '(password-store))
  136. @end lisp
  137. By adding multiple entries to @code{auth-sources} with a particular
  138. host or protocol, you can have specific netrc files for that host or
  139. protocol. Usually this is unnecessary but may make sense if you have
  140. shared netrc files or some other unusual setup (90% of Emacs users
  141. have unusual setups and the remaining 10% are @emph{really} unusual).
  142. Here's a mixed example using two sources:
  143. @lisp
  144. (setq auth-sources '((:source (:secrets default)
  145. :host "myserver" :user "joe")
  146. "~/.authinfo.gpg"))
  147. @end lisp
  148. @end defvar
  149. If you don't customize @code{auth-sources}, you'll have to live with
  150. the defaults: the unencrypted netrc file @file{~/.authinfo} will be
  151. used for any host and any port.
  152. If that fails, any host and any port are looked up in the netrc file
  153. @file{~/.authinfo.gpg}, which is a GnuPG encrypted file (@pxref{GnuPG
  154. and EasyPG Assistant Configuration}).
  155. Finally, the unencrypted netrc file @file{~/.netrc} will be used for
  156. any host and any port.
  157. The typical netrc line example is without a port.
  158. @example
  159. machine YOURMACHINE login YOU password YOURPASSWORD
  160. @end example
  161. This will match any authentication port. Simple, right? But what if
  162. there's a SMTP server on port 433 of that machine that needs a
  163. different password from the IMAP server?
  164. @example
  165. machine YOURMACHINE login YOU password SMTPPASSWORD port 433
  166. machine YOURMACHINE login YOU password GENERALPASSWORD
  167. @end example
  168. For url-auth authentication (HTTP/HTTPS), you need to put this in your
  169. netrc file:
  170. @example
  171. machine yourmachine.com:80 port http login testuser password testpass
  172. @end example
  173. This will match any realm and authentication method (basic or digest)
  174. over HTTP@. HTTPS is set up similarly. If you want finer controls,
  175. explore the url-auth source code and variables.
  176. For Tramp authentication, use:
  177. @example
  178. machine yourmachine.com port scp login testuser password testpass
  179. @end example
  180. Note that the port denotes the Tramp connection method. When you
  181. don't use a port entry, you match any Tramp method, as explained
  182. earlier. Since Tramp has about 88 connection methods, this may be
  183. necessary if you have an unusual (see earlier comment on those) setup.
  184. @node Multiple GMail accounts with Gnus
  185. @chapter Multiple GMail accounts with Gnus
  186. For multiple GMail accounts with Gnus, you have to make two nnimap
  187. entries in your @code{gnus-secondary-select-methods} with distinct
  188. names:
  189. @example
  190. (setq gnus-secondary-select-methods '((nnimap "gmail"
  191. (nnimap-address "imap.gmail.com"))
  192. (nnimap "gmail2"
  193. (nnimap-address "imap.gmail.com"))))
  194. @end example
  195. Your netrc entries will then be:
  196. @example
  197. machine gmail login account@@gmail.com password "account password" port imap
  198. machine gmail2 login account2@@gmail.com password "account2 password" port imap
  199. @end example
  200. @node Secret Service API
  201. @chapter Secret Service API
  202. The @dfn{Secret Service API} is a standard from
  203. @uref{http://www.freedesktop.org/wiki/Specifications/secret-storage-spec,,freedesktop.org}
  204. to securely store passwords and other confidential information. This
  205. API is implemented by system daemons such as the GNOME Keyring and the
  206. KDE Wallet (these are GNOME and KDE packages respectively and should
  207. be available on most modern GNU/Linux systems).
  208. The auth-source library uses the @file{secrets.el} library to connect
  209. through the Secret Service API@. You can also use that library in
  210. other packages, it's not exclusive to auth-source.
  211. @defvar secrets-enabled
  212. After loading @file{secrets.el}, a non-@code{nil} value of this
  213. variable indicates the existence of a daemon providing the Secret
  214. Service API.
  215. @end defvar
  216. @deffn Command secrets-show-secrets
  217. This command shows all collections, items, and their attributes.
  218. @end deffn
  219. The atomic objects managed by the Secret Service API are @dfn{secret
  220. items}, which contain things an application wishes to store securely,
  221. like a password. Secret items have a label (a name), the @dfn{secret}
  222. (which is the string we want, like a password), and a set of lookup
  223. attributes. The attributes can be used to search and retrieve a
  224. secret item at a later date.
  225. Secret items are grouped in @dfn{collections}. A collection is
  226. sometimes called a @samp{keyring} or @samp{wallet} in GNOME Keyring
  227. and KDE Wallet but it's the same thing, a group of secrets.
  228. Collections are personal and protected so only the owner can open them.
  229. The most common collection is called @code{"login"}.
  230. A collection can have an alias. The alias @code{"default"} is
  231. commonly used so the clients don't have to know the specific name of
  232. the collection they open. Other aliases are not supported yet.
  233. Since aliases are globally accessible, set the @code{"default"} alias
  234. only when you're sure it's appropriate.
  235. @defun secrets-list-collections
  236. This function returns all the collection names as a list.
  237. @end defun
  238. @defun secrets-set-alias collection alias
  239. Set @var{alias} as alias of collection labeled @var{collection}.
  240. Currently only the alias @code{"default"} is supported.
  241. @end defun
  242. @defun secrets-get-alias alias
  243. Return the collection name @var{alias} is referencing to.
  244. Currently only the alias @code{"default"} is supported.
  245. @end defun
  246. Collections can be created and deleted by the functions
  247. @code{secrets-create-collection} and @code{secrets-delete-collection}.
  248. Usually, this is not done from within Emacs. Do not delete standard
  249. collections such as @code{"login"}.
  250. The special collection @code{"session"} exists for the lifetime of the
  251. corresponding client session (in our case, Emacs's lifetime). It is
  252. created automatically when Emacs uses the Secret Service interface and
  253. it is deleted when Emacs is killed. Therefore, it can be used to
  254. store and retrieve secret items temporarily. The @code{"session"}
  255. collection is better than a persistent collection when the secret
  256. items should not live longer than Emacs. The session collection can
  257. be specified either by the string @code{"session"}, or by @code{nil},
  258. whenever a collection parameter is needed in the following functions.
  259. @defun secrets-list-items collection
  260. Returns all the item labels of @var{collection} as a list.
  261. @end defun
  262. @defun secrets-create-item collection item password &rest attributes
  263. This function creates a new item in @var{collection} with label
  264. @var{item} and password @var{password}. @var{attributes} are
  265. key-value pairs set for the created item. The keys are keyword
  266. symbols, starting with a colon. Example:
  267. @example
  268. ;;; The session "session", the label is "my item"
  269. ;;; and the secret (password) is "geheim"
  270. (secrets-create-item "session" "my item" "geheim"
  271. :method "sudo" :user "joe" :host "remote-host")
  272. @end example
  273. @end defun
  274. @defun secrets-get-secret collection item
  275. Return the secret of item labeled @var{item} in @var{collection}.
  276. If there is no such item, return @code{nil}.
  277. @end defun
  278. @defun secrets-delete-item collection item
  279. This function deletes item @var{item} in @var{collection}.
  280. @end defun
  281. The lookup attributes, which are specified during creation of a
  282. secret item, must be a key-value pair. Keys are keyword symbols,
  283. starting with a colon; values are strings. They can be retrieved
  284. from a given secret item and they can be used for searching of items.
  285. @defun secrets-get-attribute collection item attribute
  286. Returns the value of key @var{attribute} of item labeled @var{item} in
  287. @var{collection}. If there is no such item, or the item doesn't own
  288. this key, the function returns @code{nil}.
  289. @end defun
  290. @defun secrets-get-attributes collection item
  291. Return the lookup attributes of item labeled @var{item} in
  292. @var{collection}. If there is no such item, or the item has no
  293. attributes, it returns @code{nil}. Example:
  294. @example
  295. (secrets-get-attributes "session" "my item")
  296. @result{} ((:user . "joe") (:host ."remote-host"))
  297. @end example
  298. @end defun
  299. @defun secrets-search-items collection &rest attributes
  300. Search for the items in @var{collection} with matching
  301. @var{attributes}. The @var{attributes} are key-value pairs, as used
  302. in @code{secrets-create-item}. Example:
  303. @example
  304. (secrets-search-items "session" :user "joe")
  305. @result{} ("my item" "another item")
  306. @end example
  307. @end defun
  308. The auth-source library uses the @file{secrets.el} library and thus
  309. the Secret Service API when you specify a source matching
  310. @code{"secrets:COLLECTION"}. For instance, you could use
  311. @code{"secrets:session"} to use the @code{"session"} collection, open only
  312. for the lifetime of Emacs. Or you could use @code{"secrets:Login"} to
  313. open the @code{"Login"} collection. As a special case, you can use the
  314. symbol @code{default} in @code{auth-sources} (not a string, but a
  315. symbol) to specify the @code{"default"} alias. Here is a contrived
  316. example that sets @code{auth-sources} to search three collections and
  317. then fall back to @file{~/.authinfo.gpg}.
  318. @example
  319. (setq auth-sources '(default
  320. "secrets:session"
  321. "secrets:Login"
  322. "~/.authinfo.gpg"))
  323. @end example
  324. @node The Unix password store
  325. @chapter The Unix password store
  326. @uref{http://www.passwordstore.org,,The standard unix password
  327. manager} (or just @code{pass}) stores your passwords in
  328. @code{gpg}-protected files following the Unix philosophy.
  329. Emacs integration of @code{pass} follows the first approach suggested
  330. by the pass project itself for data organization to find data. This
  331. means that the filename of the file containing the password for a user
  332. on a particular host must contain the host name. The file itself must
  333. contain the password on the first line, as well as a @code{username}
  334. field containing the username on a subsequent line. A @code{port}
  335. field can be used to differentiate the authentication data for several
  336. services with the same username on the same host.
  337. Users of @code{pass} may also be interested in functionality provided
  338. by other Emacs packages dealing with pass:
  339. @itemize
  340. @item
  341. @uref{https://git.zx2c4.com/password-store/tree/contrib/emacs/password-store.el,,password-store}: library wrapping @code{pass};
  342. @item
  343. @uref{https://github.com/NicolasPetton/pass,,pass}: major mode to manipulate the store and edit entries;
  344. @item
  345. @uref{https://github.com/jabranham/helm-pass,,helm-pass}: helm interface for pass.
  346. @end itemize
  347. @node Help for developers
  348. @chapter Help for developers
  349. The auth-source library lets you control logging output easily.
  350. @defvar auth-source-debug
  351. Set this variable to @code{'trivia} to see lots of output in
  352. @file{*Messages*}, or set it to a function that behaves like
  353. @code{message} to do your own logging.
  354. @end defvar
  355. The auth-source library only has a few functions for external use.
  356. @defun auth-source-search &rest spec &key type max host user port secret require create delete &allow-other-keys
  357. This function searches (or modifies) authentication backends according
  358. to @var{spec}. See the function's doc-string for details.
  359. @c TODO more details.
  360. @end defun
  361. Let's take a look at an example of using @code{auth-source-search}
  362. from Gnus's @code{nnimap.el}.
  363. @example
  364. (defun nnimap-credentials (address ports)
  365. (let* ((auth-source-creation-prompts
  366. '((user . "IMAP user at %h: ")
  367. (secret . "IMAP password for %u@@%h: ")))
  368. (found (nth 0 (auth-source-search :max 1
  369. :host address
  370. :port ports
  371. :require '(:user :secret)
  372. :create t))))
  373. (if found
  374. (list (plist-get found :user)
  375. (let ((secret (plist-get found :secret)))
  376. (if (functionp secret)
  377. (funcall secret)
  378. secret))
  379. (plist-get found :save-function))
  380. nil)))
  381. @end example
  382. This call requires the user and password (secret) to be in the
  383. results. It also requests that an entry be created if it doesn't
  384. exist already. While the created entry is being assembled, the shown
  385. prompts will be used to interact with the user. The caller can also
  386. pass data in @code{auth-source-creation-defaults} to supply defaults
  387. for any of the prompts.
  388. Note that the password needs to be evaluated if it's a function. It's
  389. wrapped in a function to provide some security.
  390. Later, after a successful login, @code{nnimap.el} calls the
  391. @code{:save-function} like so:
  392. @example
  393. (when (functionp (nth 2 credentials))
  394. (funcall (nth 2 credentials)))
  395. @end example
  396. This will work whether the @code{:save-function} was provided or not.
  397. @code{:save-function} will be provided only when a new entry was
  398. created, so this effectively says ``after a successful login, save the
  399. authentication information we just used, if it was newly created.''
  400. After the first time it's called, the @code{:save-function} will not
  401. run again (but it will log something if you have set
  402. @code{auth-source-debug} to @code{'trivia}). This is so it won't ask
  403. the same question again, which is annoying. This is so it won't ask
  404. the same question again, which is annoying. This is so it won't ask
  405. the same question again, which is annoying.
  406. So the responsibility of the API user that specified @code{:create t}
  407. is to call the @code{:save-function} if it's provided.
  408. @defun auth-source-delete &rest spec &key delete &allow-other-keys
  409. This function deletes entries matching @var{spec} from the
  410. authentication backends. It returns the entries that were deleted.
  411. The backend may not actually delete the entries.
  412. @end defun
  413. @defun auth-source-forget spec
  414. This function forgets any cached data that exactly matches @var{spec}.
  415. It returns @code{t} if it forget some data, and @code{nil} if no
  416. matching data was found.
  417. @end defun
  418. @defun auth-source-forget+ &rest spec &allow-other-keys
  419. This function forgets any cached data matching @var{spec}.
  420. It returns the number of items forgotten.
  421. @end defun
  422. @node GnuPG and EasyPG Assistant Configuration
  423. @appendix GnuPG and EasyPG Assistant Configuration
  424. If the @code{auth-sources} variable contains @file{~/.authinfo.gpg}
  425. before @file{~/.authinfo}, the auth-source library will try to
  426. read the GnuPG encrypted @file{.gpg} file first, before
  427. the unencrypted file.
  428. In Emacs 23 or later there is an option @code{auto-encryption-mode} to
  429. automatically decrypt @file{*.gpg} files. It is enabled by default.
  430. If you are using earlier versions of Emacs, you will need:
  431. @lisp
  432. (require 'epa-file)
  433. (epa-file-enable)
  434. @end lisp
  435. If you want your GnuPG passwords to be cached, set up @code{gpg-agent}
  436. or EasyPG Assistant
  437. (@pxref{Caching Passphrases, , Caching Passphrases, epa}).
  438. To quick start, here are some questions:
  439. @itemize
  440. @item
  441. Do you use GnuPG version 2 instead of GnuPG version 1?
  442. @item
  443. Do you use symmetric encryption rather than public key encryption?
  444. @item
  445. Do you want to use gpg-agent?
  446. @end itemize
  447. Here are configurations depending on your answers:
  448. @multitable {111} {222} {333} {configuration configuration configuration}
  449. @item @b{1} @tab @b{2} @tab @b{3} @tab Configuration
  450. @item Yes @tab Yes @tab Yes @tab Set up gpg-agent.
  451. @item Yes @tab Yes @tab No @tab You can't, without gpg-agent.
  452. @item Yes @tab No @tab Yes @tab Set up gpg-agent.
  453. @item Yes @tab No @tab No @tab You can't, without gpg-agent.
  454. @item No @tab Yes @tab Yes @tab Set up elisp passphrase cache.
  455. @item No @tab Yes @tab No @tab Set up elisp passphrase cache.
  456. @item No @tab No @tab Yes @tab Set up gpg-agent.
  457. @item No @tab No @tab No @tab You can't, without gpg-agent.
  458. @end multitable
  459. To set up gpg-agent, follow the instruction in GnuPG manual
  460. (@pxref{Invoking GPG-AGENT, , Invoking GPG-AGENT, gnupg}).
  461. To set up elisp passphrase cache, set
  462. @code{epa-file-cache-passphrase-for-symmetric-encryption}.
  463. @node GNU Free Documentation License
  464. @appendix GNU Free Documentation License
  465. @include doclicense.texi
  466. @node Index
  467. @unnumbered Index
  468. @printindex cp
  469. @node Function Index
  470. @unnumbered Function Index
  471. @printindex fn
  472. @node Variable Index
  473. @unnumbered Variable Index
  474. @printindex vr
  475. @bye
  476. @c End: