Browse Source

Implement a secret-agent

master
Damien Cassou 2 years ago
parent
commit
2807325fb1
Signed by: DamienCassou GPG Key ID: B68746238E59B548
1 changed files with 80 additions and 26 deletions
  1. +80
    -26
      libnetmacs.el

+ 80
- 26
libnetmacs.el View File

@ -30,6 +30,9 @@
(require 'dbus)
(eval-when-compile
(require 'subr-x)) ; when-let*
;; Private variables
@ -487,32 +490,83 @@ If not nil, call HANDLER when scanning is finished."
;; Secret agent
;; (defun libnetmacs--agent-method-get-secrets (connection connection-path setting-name hints flags)
;; "CONNECTION CONNECTION-PATH SETTING-NAME HINTS FLAGS."
;; (message "GetSecrets: connection = %s" connection)
;; (message "GetSecrets: connection-path = %s" connection-path)
;; (message "GetSecrets: setting-name = %s" setting-name)
;; (message "GetSecrets: hints = %s" hints)
;; (message "GetSecrets: flags = %s" flags)
;; '())
;; (defvar libnetmacs--agent-get-secrets-registration nil)
;; (defun libnetmacs-agent-register ()
;; "Register libnetmacs as a NetworkManager SecretAgent."
;; (setq libnetmacs--agent-get-secrets-registration
;; (dbus-register-method
;; libnetmacs--bus
;; libnetmacs--service
;; "/org/freedesktop/NetworkManager/SecretAgent"
;; "org.freedesktop.NetworkManager.SecretAgent"
;; "GetSecrets"
;; #'libnetmacs--agent-method-get-secrets))
;; (libnetmacs--call "Register" '("org.libnetmacs") "AgentManager" "AgentManager"))
;; (libnetmacs--call
;; "GetSecrets" '("") "/org/freedesktop/NetworkManager/Settings/57" "Connection")
(defgroup libnetmacs nil
"Library to communicate with NetworkManager."
:group 'comm)
(defcustom libnetmacs-secretagent-nmcli-command "nmcli"
"Path to nmcli command."
:group 'libnetmacs
:type 'string)
(defvar libnetmacs--secretagent-process nil
"Holds information about the SecretAgent proccess.")
(defun libnetmacs--secretagent-get-password (connection-name)
"Return a password for CONNECTION-NAME."
(when-let* ((entries (auth-source-search :host connection-name :port "NetworkManager"))
(entry (car entries))
(secret (plist-get entry :secret))
(secret (if (functionp secret) (funcall secret) secret)))
secret))
(defvar libnetmacs--secretagent-password-regex
(rx
(or "to connect to" "to access the wireless network")
" '"
(group-n 1 (1+ (not (any "'"))))
"'")
"Regular expression matching password prompts.
This regex puts the requested password name into group 1.")
(defun libnetmacs--secretagent-filter (process input)
"Handle INPUT from PROCESS."
(let ((process-buffer (process-buffer process)))
(with-temp-buffer
(message "secret-agent received: %S" input)
(insert input)
(goto-char (point-min))
(when (re-search-forward libnetmacs--secretagent-password-regex nil t)
(let* ((connection-name (match-string-no-properties 1))
(password (libnetmacs--secretagent-get-password connection-name)))
(process-send-string process (format "%s\n" (or password "")))
(with-current-buffer process-buffer
(insert (format "Got a request for a password for '%s'. Password %sfound\n"
connection-name
(if password "" "not ")))))))))
(defun libnetmacs-start-secretagent ()
"Use Emacs' auth-source to answer NetworkManager's password requests.
This requires nmcli to be installed."
(interactive)
(when libnetmacs--secretagent-process
(libnetmacs-stop-secretagent))
(setq libnetmacs--secretagent-process
(make-process
:name "libnetmacs-secretagent"
:buffer (get-buffer-create "*libnetmacs-secretagent*")
:command (list (executable-find libnetmacs-secretagent-nmcli-command) "agent" "secret")
:noquery t
:filter #'libnetmacs--secretagent-filter
:sentinel (lambda (_ event)
(message "secretagent is '%s'" event)))))
(defun libnetmacs-stop-secretagent ()
"Stop libnetmacs' SecretAgent.
This requires nmcli to be installed."
(interactive)
(ignore-errors (stop-process libnetmacs--secretagent-process))
(setq libnetmacs--secretagent-process nil))
(define-minor-mode libnetmacs-secretagent-mode
"Use Emacs' auth-source to answer NetworkManager's password requests."
:group 'libnetmacs
:global t
:init-value nil
:require 'libnetmacs
(if libnetmacs-secretagent-mode
(libnetmacs-start-secretagent)
(libnetmacs-stop-secretagent)))
(provide 'libnetmacs)
;;; libnetmacs.el ends here

Loading…
Cancel
Save