Debian Codegraph @

mew-ssl.el

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 ;;; mew-ssl.el
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Jul 25, 2002
5
6 ;;; Code:
7
8 (require 'mew)
9
10 (defvar mew-prog-ssl "stunnel")
11 (defvar mew-ssl-cert-directory "~/.certs"
12 "The directory where certificates of root servers are stored.
13 A file name of a certificate should be 'cert-hash.0'.
14 'cert-hash' can be extracted by 'openssl x509 -hash -noout -in cert.pem'.")
15
16 (defvar mew-ssl-verify-level 1
17 "Verification level of server's certificate.
18 0 - no verification.
19 1 - verify server's certificate if present. If verification failed, an
20 SSL/TLS connection is not created. If not present, an SSL/TLS connection
21 is created.
22 2 - verify server's certificate. If verification failed, an SSL/TLS
23 connection is not created. If not present, an SSL/TLS connection is
24 not created.
25 3 - verify server's certificate which locally installed (not one from
26 the server).")
27
28 (defvar mew-prog-ssl-arg nil
29 "For stunnel v3, a list of command-line arguments, each one a string.
30 For stunnel v4 or v5, a string of extra text to place in the configuration
31 file, which should end with a newline (example: \"fips=no\\n\"); or nil to
32 insert no extra text.")
33
34 (defvar mew-ssl-ver nil)
35 (defvar mew-ssl-minor-ver nil)
36
37 (defvar mew-ssl-libwrap nil)
38 (defvar mew-ssl-unixlike nil
39 "Set to t when stunnel supports \"foreground\" option.")
40 (defvar mew-ssl-checkhost nil)
41
42 (defconst mew-ssl-process-exec-cnt 3)
43
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;;
46 ;;; Magic words
47 ;;;
48
49 (defconst mew-tls-smtp "smtp")
50 (defconst mew-tls-pop "pop3")
51 (defconst mew-tls-nntp "nntp")
52 (defconst mew-tls-imap "imap") ;; xxx stunnel does not support this.
53
54 ;; stunnel does not support IPv6, sigh
55 (defconst mew-ssl-localhost "127.0.0.1")
56
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 ;;;
59 ;;; SSL/TLS info
60 ;;;
61
62 (defvar mew-ssl-info-list '("status" "try" "file" "string"))
63
64 (mew-info-defun "mew-ssl-" mew-ssl-info-list)
65
66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67 ;;;
68 ;;; Process name
69 ;;;
70
71 (defconst mew-ssl-info-prefix "mew-ssl-info-")
72
73 (defun mew-ssl-info-name (server remoteport localport)
74 (format "%s:%s:%d:%d" mew-ssl-info-prefix server remoteport localport))
75
76 (defun mew-ssl-info-name-regex (server remoteport)
77 (format "^%s:%s:%d" mew-ssl-info-prefix server remoteport))
78
79 (defun mew-ssl-pnm-to-lport (pnm)
80 (if (string-match ":\\([0-9]+\\)$" pnm) (match-string 1 pnm)))
81
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 ;;;
84 ;;; Executing SSL/TLS
85 ;;;
86
87 (defun mew-ssl-server (server)
88 (if (string= server "localhost")
89 mew-ssl-localhost
90 server))
91
92 (defun mew-ssl-options (case server remoteport localport tls)
93 (setq server (mew-ssl-server server))
94 (if (= mew-ssl-ver 3)
95 (let (args)
96 (setq args
97 `("-c" "-f"
98 "-a" ,(expand-file-name (mew-ssl-cert-directory case))
99 "-d" ,(format "%s:%d" mew-ssl-localhost localport)
100 "-v" ,(number-to-string (mew-ssl-verify-level case))
101 "-D" "debug"
102 "-P" ""
103 "-r" ,(format "%s:%d" server remoteport)
104 ,@mew-prog-ssl-arg))
105 (if tls (setq args (cons "-n" (cons tls args))))
106 args)
107 (let ((file (mew-make-temp-name)))
108 (with-temp-buffer
109 (insert "client=yes\n")
110 (if mew-ssl-unixlike
111 (insert "pid=\n"))
112 (insert (format "verify=%d\n" (mew-ssl-verify-level case)))
113 (if (and (> (mew-ssl-verify-level case) 0) mew-ssl-checkhost)
114 (insert (format "checkHost=%s\n" server)))
115 (if mew-ssl-unixlike
116 (insert "foreground=yes\n"))
117 (insert "debug=debug\n")
118 (if (and mew-ssl-libwrap (or (>= mew-ssl-ver 5) (>= mew-ssl-minor-ver 45)))
119 (insert "libwrap=no\n"))
120 (if (and (or (>= mew-ssl-ver 5) (>= mew-ssl-minor-ver 22))
121 mew-ssl-unixlike)
122 (insert "syslog=no\n"))
123 (insert "CApath=" (expand-file-name (mew-ssl-cert-directory case)) "\n")
124 (if mew-prog-ssl-arg
125 (insert mew-prog-ssl-arg))
126 (insert (format "[%d]\n" localport))
127 (insert (format "accept=%s:%d\n" mew-ssl-localhost localport))
128 (insert (format "connect=%s:%d\n" server remoteport))
129 (if tls (insert (format "protocol=%s\n" tls)))
130 (mew-frwlet mew-cs-dummy mew-cs-text-for-write
131 ;; NEVER use call-process-region for privacy reasons
132 (write-region (point-min) (point-max) file nil 'no-msg))
133 (list file)))))
134
135 (defun mew-open-ssl-stream (case server serv tls)
136 "Open an SSL/TLS stream for SERVER's SERV.
137 This function returns a process when an SSL/TLS connection is created
138 successfully.
139 If TLS is nil, an SSL connection is created.
140 If TLS is a magic word for 'stunnel', a TLS connection is created.
141 A local port number can be obtained the process name after ':'. "
142 (cond
143 ((or (null mew-ssl-ver) (not (mew-which-exec mew-prog-ssl)))
144 (message "'%s' is not found" mew-prog-ssl)
145 nil)
146 (t
147 (let* ((remoteport (mew-serv-to-port serv))
148 (localport (+ 8000 (% (mew-random) 4000)))
149 (process-connection-type mew-connection-type2)
150 (N mew-ssl-process-exec-cnt)
151 (pros (process-list))
152 (regex (mew-ssl-info-name-regex server remoteport))
153 name pnm pro dummy bound opts)
154 (catch 'find
155 (dolist (pr pros)
156 (when (string-match regex (process-name pr))
157 (if (memq (process-status pr) '(run))
158 (setq pro pr)
159 (delete-process pr))
160 (throw 'find nil))))
161 (if pro
162 pro
163 (message "Creating an SSL/TLS connection...")
164 (setq pro nil)
165 (catch 'loop
166 (dotimes (_i N) ;; prevent byte-compile warning
167 (setq name (mew-ssl-info-name server remoteport localport))
168 (setq opts (mew-ssl-options case server remoteport localport tls))
169 (setq pro (apply 'start-process name nil mew-prog-ssl opts))
170 ;; An error would occur. So, let's exit in the case.
171 (cond
172 ((not (processp pro))
173 (message "Creating an SSL/TLS connection...FAILED")
174 (throw 'loop nil))
175 ((not (memq (process-status pro) '(run)))
176 (delete-process pro)
177 (message "Creating an SSL/TLS connection...FAILED")
178 (throw 'loop nil)))
179 ;; stunnel is now running.
180 (mew-process-silent-exit pro)
181 (setq pnm (process-name pro))
182 (mew-info-clean-up pnm)
183 (mew-ssl-set-try pnm 0)
184 (if (>= mew-ssl-ver 4) (mew-ssl-set-file pnm (car opts)))
185 (mew-set-process-cs pro mew-cs-text-for-read mew-cs-text-for-write)
186 (set-process-filter pro 'mew-ssl-filter1)
187 (set-process-sentinel pro 'mew-ssl-sentinel)
188 (mew-rendezvous (null (mew-ssl-get-status pnm)))
189 (if (eq (mew-ssl-get-status pnm) t)
190 (throw 'loop (setq bound t)))
191 ;; bind-failure
192 (setq localport (1+ localport))))
193 (mew-ssl-set-status pnm nil)
194 (if (not bound)
195 (progn
196 (message "Creating an SSL/TLS connection...FAILED")
197 nil)
198 ;; "stunnel" does not gain access to the remote port
199 ;; until a tunneled connection is created.
200 ;; So, we need to check the SSL/TLS tunnel with a dummy
201 ;; tunneled connection here.
202 (set-process-filter pro 'mew-ssl-filter2)
203 (setq dummy (open-network-stream " *Mew dummy*" nil mew-ssl-localhost localport))
204 (mew-rendezvous (null (mew-ssl-get-status pnm)))
205 (if (processp dummy) (delete-process dummy))
206 (if (eq (mew-ssl-get-status pnm) t)
207 (progn
208 (message "Creating an SSL/TLS connection...done")
209 (set-process-filter pro 'mew-ssl-filter3)
210 pro)
211 ;; verify-failure
212 (delete-process pro)
213 (message "Creating an SSL/TLS connection...FAILED (cert verify failure)")
214 nil)))))))
215
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 ;;;
218 ;;; Filter and sentinel
219 ;;;
220
221 (defun mew-ssl-debug (label string)
222 (when (mew-debug 'net)
223 (with-current-buffer (get-buffer-create mew-buffer-debug)
224 (goto-char (point-max))
225 (insert (format "\n<%s>\n%s\n" label string)))))
226
227 (defun mew-ssl-filter1 (process string)
228 (let* ((pnm (process-name process))
229 (prev-str (mew-ssl-get-string pnm)))
230 (save-excursion
231 (mew-ssl-debug "SSL/TLS: " string)
232 (mew-ssl-set-string pnm string)
233 (setq string (concat prev-str string))
234 (cond
235 ((string-match "bound \\(\\|FD=[0-9]+ \\)to" string)
236 (mew-ssl-set-status pnm t))
237 ((string-match "gethostbyname: Valid name, no data record of requested type" string)
238 (mew-ssl-set-status pnm 'gethostbyname-failure))
239 ((string-match "gethostbyname: Host not found" string)
240 (mew-ssl-set-status pnm 'gethostbyname-failure))
241 ((string-match "Local: bind: Address already in use" string)
242 (mew-ssl-set-status pnm 'bind-failure))))))
243
244 (defun mew-ssl-filter2 (process string)
245 (let* ((pnm (process-name process))
246 (prev-str (mew-ssl-get-string pnm)))
247 (save-excursion
248 (mew-ssl-debug "SSL/TLS: " string)
249 (mew-ssl-set-string pnm string)
250 (setq string (concat prev-str string))
251 (cond
252 ((string-match "Negotiated \\| ciphersuite:\\|opened with SSL" string)
253 (mew-ssl-set-status pnm t))
254 ((string-match "Failed to initialize" string)
255 (mew-ssl-set-status pnm t)) ;; xxx
256 ((string-match "verify failed" string)
257 (mew-ssl-set-status pnm 'verify-failure))))))
258
259 (defun mew-ssl-filter3 (_process string)
260 (save-excursion
261 (mew-ssl-debug "SSL/TLS: " string)))
262
263 (defun mew-ssl-sentinel (process _event)
264 (let* ((pnm (process-name process))
265 (file (mew-ssl-get-file pnm)))
266 (save-excursion
267 (mew-delete-file file))))
268
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;;;
271 ;;; stunnel version check
272 ;;;
273
274 (defun mew-ssl-setup ()
275 (if (not (mew-which-exec mew-prog-ssl))
276 (setq mew-ssl-ver nil)
277 (with-temp-buffer
278 (call-process mew-prog-ssl nil t nil "-help")
279 (goto-char (point-min))
280 (re-search-forward "^stunnel " nil t 1)
281 (if (looking-at "\\([45]\\)\\.\\([0-9]+\\)")
282 (progn
283 (setq mew-ssl-ver (string-to-number (mew-match-string 1)))
284 (setq mew-ssl-minor-ver (string-to-number (mew-match-string 2))))
285 (setq mew-ssl-ver 3))
286 (when (re-search-forward "LIBWRAP" nil t)
287 (setq mew-ssl-libwrap t))
288 (when (re-search-forward "foreground" nil t)
289 (setq mew-ssl-unixlike t))
290 (when (re-search-forward "checkHost" nil t)
291 (setq mew-ssl-checkhost t)))))
292
293 (provide 'mew-ssl)
294
295 ;;; Copyright Notice:
296
297 ;; Copyright (C) 2002-2018 Mew developing team.
298 ;; All rights reserved.
299
300 ;; Redistribution and use in source and binary forms, with or without
301 ;; modification, are permitted provided that the following conditions
302 ;; are met:
303 ;;
304 ;; 1. Redistributions of source code must retain the above copyright
305 ;; notice, this list of conditions and the following disclaimer.
306 ;; 2. Redistributions in binary form must reproduce the above copyright
307 ;; notice, this list of conditions and the following disclaimer in the
308 ;; documentation and/or other materials provided with the distribution.
309 ;; 3. Neither the name of the team nor the names of its contributors
310 ;; may be used to endorse or promote products derived from this software
311 ;; without specific prior written permission.
312 ;;
313 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
314 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
315 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
316 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
317 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
318 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
319 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
320 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
321 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
322 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
323 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
324
325 ;;; mew-ssl.el ends here