| 1 |
;;; mew-ssh.el |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Dec 14, 1999 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
(defvar mew-ssh-prog-ver nil) ;; 0-SSH1, 1-SSH2, 2-OpenSSH, 3-PuTTY |
| 11 |
|
| 12 |
(defun mew-ssh-get (case list-or-vec) |
| 13 |
(elt list-or-vec (mew-ssh-prog-ver case))) |
| 14 |
|
| 15 |
(defconst mew-ssh-msg-passwd |
| 16 |
'("password:\\|Enter passphrase" |
| 17 |
"password:\\|Passphrase for\\|Enter passphrase" |
| 18 |
"password:\\|Enter passphrase" |
| 19 |
"Password:\\|Passphrase for key")) |
| 20 |
|
| 21 |
(defconst mew-ssh-msg-connected |
| 22 |
'("Entering interactive session" |
| 23 |
"Authentication successful\\|client_authenticated" |
| 24 |
"Entering interactive session" |
| 25 |
"Access granted")) |
| 26 |
|
| 27 |
(defconst mew-ssh-msg-denied |
| 28 |
'("Permission denied\\." "Permission denied\\." "Permission denied\\." |
| 29 |
"Permission denied\\.")) ;XXX |
| 30 |
|
| 31 |
(defconst mew-ssh-msg-refused |
| 32 |
'("Secure connection .* refused\\." |
| 33 |
"FATAL: Connecting .* failed:" |
| 34 |
"Secure connection .* refused\\." |
| 35 |
"Secure connection .* refused\\.")) ;XXX |
| 36 |
|
| 37 |
(defconst mew-ssh-process-exec-cnt 3) |
| 38 |
|
| 39 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 40 |
;;; |
| 41 |
;;; SSH info |
| 42 |
;;; |
| 43 |
|
| 44 |
(defvar mew-ssh-info-list '("case" "status" "try" "account")) |
| 45 |
|
| 46 |
(mew-info-defun "mew-ssh-" mew-ssh-info-list) |
| 47 |
|
| 48 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 49 |
;;; |
| 50 |
;;; Process name |
| 51 |
;;; |
| 52 |
|
| 53 |
(defconst mew-ssh-info-prefix "mew-ssh-info-") |
| 54 |
|
| 55 |
(defun mew-ssh-info-name (sshserver server remoteport localport) |
| 56 |
(format "%s:%s:%s:%d:%d" |
| 57 |
mew-ssh-info-prefix sshserver server remoteport localport)) |
| 58 |
|
| 59 |
(defun mew-ssh-info-name-regex (sshserver server remoteport) |
| 60 |
(format "^%s:%s:%s:%d" mew-ssh-info-prefix sshserver server remoteport)) |
| 61 |
|
| 62 |
(defun mew-ssh-pnm-to-lport (pnm) |
| 63 |
(if (string-match ":\\([0-9]+\\)$" pnm) (match-string 1 pnm))) |
| 64 |
|
| 65 |
;; user@host:port should be used here. |
| 66 |
;; However, SSH does not symbolic port name "ssh" for the "-p" option |
| 67 |
;; while OpenSSH does. Sigh... |
| 68 |
(defun mew-ssh-passtag (name) |
| 69 |
(if (string-match (format "^%s:[^:]+:" mew-ssh-info-prefix) name) |
| 70 |
(match-string 0 name))) |
| 71 |
|
| 72 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 73 |
;;; |
| 74 |
;;; Executing SSH |
| 75 |
;;; |
| 76 |
|
| 77 |
(defun mew-open-ssh-stream (case server serv sshserver) |
| 78 |
"Open SSH stream for SERVER's SERV via SSHSERVER. |
| 79 |
This function returns a process when an SSH connection is created |
| 80 |
successfully. A local port number can be obtained the process name |
| 81 |
after ':'." |
| 82 |
(let* ((ssh (mew-ssh-prog case)) |
| 83 |
(args (mew-ssh-prog-args case)) |
| 84 |
(remoteport (mew-serv-to-port serv)) |
| 85 |
(localport (+ 8000 (% (mew-random) 4000))) |
| 86 |
(process-connection-type mew-connection-type2) |
| 87 |
(N mew-ssh-process-exec-cnt) |
| 88 |
(pros (process-list)) |
| 89 |
(regex (mew-ssh-info-name-regex sshserver server remoteport)) |
| 90 |
name pnm pro status) |
| 91 |
(cond |
| 92 |
((not (mew-which-exec ssh)) |
| 93 |
(message "'%s' is not found" ssh)) |
| 94 |
(t |
| 95 |
(catch 'find |
| 96 |
(dolist (pr pros) |
| 97 |
(when (string-match regex (process-name pr)) |
| 98 |
(if (memq (process-status pr) '(run)) |
| 99 |
(setq pro pr) |
| 100 |
(delete-process pr)) |
| 101 |
(throw 'find nil)))) |
| 102 |
(if pro |
| 103 |
pro |
| 104 |
(message "Connecting to the SSH server...") |
| 105 |
(setq pro nil) |
| 106 |
(catch 'loop |
| 107 |
(dotimes (_i N) ;; prevent byte-compile warning |
| 108 |
(setq name (mew-ssh-info-name sshserver server remoteport localport)) |
| 109 |
(setq pro (apply 'start-process |
| 110 |
name nil |
| 111 |
ssh "-x" "-v" "-L" |
| 112 |
(format "%d:%s:%d" localport server remoteport) |
| 113 |
(append args (list sshserver)))) |
| 114 |
;; An error would occur. So, let's exit in the case. |
| 115 |
(mew-timing) |
| 116 |
(cond |
| 117 |
((not (processp pro)) |
| 118 |
(message "Connecting to the SSH server...FAILED") |
| 119 |
(throw 'loop nil)) |
| 120 |
((not (memq (process-status pro) '(run))) |
| 121 |
(delete-process pro) |
| 122 |
(message "Connecting to the SSH server...FAILED") |
| 123 |
(throw 'loop nil))) |
| 124 |
;; ssh is now running. |
| 125 |
(mew-process-silent-exit pro) |
| 126 |
(setq pnm (process-name pro)) |
| 127 |
(mew-info-clean-up pnm) |
| 128 |
(mew-ssh-set-try pnm 0) |
| 129 |
(mew-ssh-set-case pnm case) |
| 130 |
(mew-ssh-set-account pnm (format "%s" sshserver)) |
| 131 |
(mew-set-process-cs pro mew-cs-text-for-read mew-cs-text-for-write) |
| 132 |
(set-process-filter pro 'mew-ssh-filter) |
| 133 |
(set-process-sentinel pro 'mew-ssh-sentinel) |
| 134 |
(mew-rendezvous (null (mew-ssh-get-status pnm))) |
| 135 |
(setq status (mew-ssh-get-status pnm)) |
| 136 |
(cond |
| 137 |
((eq status t) |
| 138 |
(throw 'loop pro)) ;; return value |
| 139 |
((not (eq status 'bound)) |
| 140 |
(message "Connecting to the SSH server...FAILED") |
| 141 |
(throw 'loop nil))) ;; return value |
| 142 |
;; 'bound |
| 143 |
(setq localport (1+ localport)) |
| 144 |
(message "Connecting to the SSH server...FAILED") |
| 145 |
nil))))))) ;; return value |
| 146 |
|
| 147 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 148 |
;;; |
| 149 |
;;; Filter and sentinel |
| 150 |
;;; |
| 151 |
|
| 152 |
(defun mew-ssh-debug (label string) |
| 153 |
(when (mew-debug 'net) |
| 154 |
(with-current-buffer (get-buffer-create mew-buffer-debug) |
| 155 |
(goto-char (point-max)) |
| 156 |
(insert (format "\n<%s>\n%s\n" label string))))) |
| 157 |
|
| 158 |
(defun mew-ssh-filter (process string) |
| 159 |
(save-excursion |
| 160 |
(mew-ssh-debug "SSH: " string) |
| 161 |
(let* ((pnm (process-name process)) |
| 162 |
(case (mew-ssh-get-case pnm)) |
| 163 |
pass) |
| 164 |
(cond |
| 165 |
((string-match (mew-ssh-get case mew-ssh-msg-passwd) string) |
| 166 |
(if (= (mew-ssh-get-try pnm) 0) |
| 167 |
(setq pass (mew-input-passwd (format "SSH password (%s): " |
| 168 |
(mew-ssh-get-account pnm)) |
| 169 |
(mew-ssh-passtag pnm))) |
| 170 |
(mew-passwd-set-passwd (mew-ssh-passtag pnm) nil) |
| 171 |
(setq pass (mew-input-passwd (format "SSH password again (%s): " |
| 172 |
(mew-ssh-get-account pnm)) |
| 173 |
(mew-ssh-passtag pnm)))) |
| 174 |
(mew-ssh-set-try pnm (1+ (mew-ssh-get-try pnm))) |
| 175 |
(if (and (processp process) |
| 176 |
(memq (process-status process) '(run))) |
| 177 |
(progn |
| 178 |
(message "Sending password to the SSH server...") |
| 179 |
(process-send-string process (concat pass "\n"))) |
| 180 |
(mew-passwd-set-passwd (mew-ssh-passtag pnm) nil) |
| 181 |
(message "Cannot find SSH process"))) |
| 182 |
((or (string-match (mew-ssh-get case mew-ssh-msg-denied) string) |
| 183 |
(string-match (mew-ssh-get case mew-ssh-msg-refused) string)) |
| 184 |
(message "Cannot connect to the SSH server") |
| 185 |
(mew-passwd-set-passwd (mew-ssh-passtag pnm) nil) |
| 186 |
(mew-ssh-set-status pnm 'denied)) |
| 187 |
((string-match (mew-ssh-get case mew-ssh-msg-connected) string) |
| 188 |
(mew-ssh-set-status pnm t) |
| 189 |
(message "Connecting to the SSH server...done")) |
| 190 |
((string-match "Local: bind: Address already in use" string) |
| 191 |
(mew-ssh-set-status pnm 'bound) |
| 192 |
(message "Connecting to the SSH server...done")))))) |
| 193 |
|
| 194 |
(defun mew-ssh-sentinel (process _event) |
| 195 |
(let ((pnm (process-name process))) |
| 196 |
(save-excursion |
| 197 |
(unless (mew-ssh-get-status pnm) |
| 198 |
(mew-ssh-set-status pnm 'exit))))) |
| 199 |
|
| 200 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 201 |
;;; |
| 202 |
;;; SSH version check |
| 203 |
;;; |
| 204 |
|
| 205 |
(defun mew-ssh-setup () |
| 206 |
(let ((ssh (mew-ssh-prog))) |
| 207 |
(if (not (mew-which-exec ssh)) |
| 208 |
(setq mew-ssh-prog-ver nil) |
| 209 |
(with-temp-buffer |
| 210 |
(call-process ssh nil t nil "-V") |
| 211 |
(goto-char (point-min)) |
| 212 |
(cond |
| 213 |
((looking-at "SSH Version 1") |
| 214 |
(setq mew-ssh-prog-ver 0)) |
| 215 |
((looking-at (concat ssh ": SSH Version 2")) |
| 216 |
(setq mew-ssh-prog-ver 1)) |
| 217 |
((looking-at (concat ssh ": SSH Secure Shell [23]")) |
| 218 |
(setq mew-ssh-prog-ver 1)) |
| 219 |
((looking-at "\\(SSH Version \\)?\\(OpenSSH\\|NetBSD\\|Sun_SSH\\)") |
| 220 |
(setq mew-ssh-prog-ver 2)) |
| 221 |
((looking-at "plink:") |
| 222 |
(setq mew-ssh-prog-ver 3)) |
| 223 |
(t |
| 224 |
(setq mew-ssh-prog-ver nil))))))) |
| 225 |
|
| 226 |
(provide 'mew-ssh) |
| 227 |
|
| 228 |
;;; Copyright Notice: |
| 229 |
|
| 230 |
;; Copyright (C) 1999-2015 Mew developing team. |
| 231 |
;; All rights reserved. |
| 232 |
|
| 233 |
;; Redistribution and use in source and binary forms, with or without |
| 234 |
;; modification, are permitted provided that the following conditions |
| 235 |
;; are met: |
| 236 |
;; |
| 237 |
;; 1. Redistributions of source code must retain the above copyright |
| 238 |
;; notice, this list of conditions and the following disclaimer. |
| 239 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 240 |
;; notice, this list of conditions and the following disclaimer in the |
| 241 |
;; documentation and/or other materials provided with the distribution. |
| 242 |
;; 3. Neither the name of the team nor the names of its contributors |
| 243 |
;; may be used to endorse or promote products derived from this software |
| 244 |
;; without specific prior written permission. |
| 245 |
;; |
| 246 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 247 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 248 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 249 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 250 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 251 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 252 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 253 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 254 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 255 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 256 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 257 |
|
| 258 |
;;; mew-ssh.el ends here |