diff options
| author | Bruno Félix Rezende Ribeiro <oitofelix@gnu.org> | 2019-08-23 06:56:00 +0300 |
|---|---|---|
| committer | Oleg Pykhalov <go.wigust@gmail.com> | 2019-08-23 07:37:02 +0300 |
| commit | 20216fca1f2f7c5d9beec8bda59e56c91ee07d41 (patch) | |
| tree | 84f435a4d5ad1af4baf1f60b0f99bf39bbe0bd48 /tramp-auto-auth.el | |
| download | emacs-tramp-auto-auth-20216fca1f2f7c5d9beec8bda59e56c91ee07d41.tar.gz | |
* tramp-auto-auth.el: New file.
Signed-off-by: Oleg Pykhalov <go.wigust@gmail.com>
Diffstat (limited to 'tramp-auto-auth.el')
| -rw-r--r-- | tramp-auto-auth.el | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/tramp-auto-auth.el b/tramp-auto-auth.el new file mode 100644 index 0000000..48c2893 --- /dev/null +++ b/tramp-auto-auth.el @@ -0,0 +1,113 @@ +;;; tramp-auto-auth.el --- TRAMP automatic authentication library + +;; Copyright (C) 2019 Bruno Félix Rezende Ribeiro <oitofelix@gnu.org> + +;; Author: Bruno Félix Rezende Ribeiro <oitofelix@gnu.org> +;; Maintainer: Bruno Félix Rezende Ribeiro <oitofelix@gnu.org> +;; Keywords: comm, processes +;; Package: tramp-auto-auth + +;; Package-Requires: (tramp) + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;; This library provides ‘tramp-auto-auth-mode’: a global minor mode +;; whose purpose is to automatically feed TRAMP sub-processes with +;; passwords for paths matching regexps. This is useful in situations +;; where interactive user input is not desirable or feasible. For +;; instance, in sub-nets with large number of hosts or whose hosts +;; have dynamic IPs assigned to them. In those cases it’s not +;; practical to query passwords using the ‘auth-source’ library, since +;; this would require each host to be listed explicitly and immutably +;; in a Netrc file. Another scenario where this mode is useful are +;; non-interactive Emacs sessions (like those used for batch +;; processing or by evaluating ‘:async’ Org Babel source blocks) in +;; which it’s impossible for the user to answer a password-asking +;; prompt. +;; +;; To make use of the automatic authentication feature, the variable +;; ‘tramp-auto-auth-alist’ must be customized to hold the path regexps +;; and their respective passwords, and then ‘tramp-auto-auth-mode’ +;; must be enabled. For example: +;; +;; (require ’tramp-auto-auth) +;; (add-to-list 'tramp-auto-auth-alist '("root@10\\.0\\." . "$r00tPasWD!")) +;; (tramp-auto-auth-mode) + +;;; Code: + + +(require 'tramp) + + +(defcustom tramp-auto-auth-alist + nil + "Alist of TRAMP paths regexps and their respective passwords. +Each element has the form (PATH-REGEXP . PASSWORD), where +PATH-REGEXP is a regular expression to be matched against TRAMP +paths and PASSWORD is the respective password to be sent to the +TRAMP’s sub-process in case a match does occur." + :type '(alist + :key-type + (string :tag "Path Regexp" + :help-echo "Regexp which matches the desired TRAMP path") + :value-type + (string :tag "Password" + :help-echo "Password for the TRAMP path resource")) + :group 'tramp + :require 'tramp-auto-auth) + +;;;###autoload +(define-minor-mode tramp-auto-auth-mode + "Toggle Tramp-Auto-Auth global minor mode on or off. +With a prefix argument ARG, enable Tramp-Auto-Auth mode if ARG is +positive, and disable it otherwise. If called from Lisp, enable +the mode if ARG is omitted or nil, and toggle it if ARG is ‘toggle’. + +When enabled ‘tramp-auto-auth-alist’ is used to automatically +authenticate to remote servers." + :group 'tramp + :global t + :require 'tramp-auto-auth + (if tramp-auto-auth-mode + (progn + (advice-add #'tramp-action-password :around + (lambda (tramp-action-password proc vec) + (pcase (or (car (last vec)) "") + ((and (app (lambda (expval) + (assoc-default expval tramp-auto-auth-alist + #'string-match-p)) + passwd) + (guard passwd)) + (process-send-string proc (concat passwd tramp-local-end-of-line))) + (_ (funcall tramp-action-password proc vec)))) + '((name . tramp-auto-auth-mode))) + (advice-add #'tramp-action-yesno :around + (lambda (tramp-action-yesno proc vec) + (pcase (or (car (last vec)) "") + ((pred (lambda (expval) + (assoc-default expval tramp-auto-auth-alist + #'string-match-p))) + (tramp-send-string vec (concat "yes" tramp-local-end-of-line))) + (_ (funcall tramp-action-yesno proc vec)))) + '((name . tramp-auto-auth-mode)))) + (advice-remove #'tramp-action-password 'tramp-auto-auth-mode) + (advice-remove #'tramp-action-yesno 'tramp-auto-auth-mode))) + + +(provide 'tramp-auto-auth) + +;;; tramp-auto-auth.el ends here |
