summaryrefslogtreecommitdiff
path: root/lisp/gnus/gssapi.el
blob: b569c7f16c614bc0382414a9d38573cfb210e2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
;;; gssapi.el --- GSSAPI/Kerberos 5 interface for Emacs

;; Copyright (C) 2011-2017 Free Software Foundation, Inc.

;; Author: Simon Josefsson <simon@josefsson.org>
;;         Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: network

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'format-spec)

(defcustom gssapi-program (list
			   (concat "gsasl %s %p "
				   "--mechanism GSSAPI "
				   "--authentication-id %l")
			   "imtest -m gssapi -u %l -p %p %s")
  "List of strings containing commands for GSSAPI (krb5) authentication.
%s is replaced with server hostname, %p with port to connect to,
and %l with the user name.  The program should accept commands on
stdin and return responses to stdout.  Each entry in the list is
tried until a successful connection is made."
  :version "24.1"
  :group 'network
  :type '(repeat string))

(defun open-gssapi-stream (name buffer server port user)
  (let ((cmds gssapi-program)
	cmd done)
    (with-current-buffer buffer
      (while (and (not done)
		  (setq cmd (pop cmds)))
	(message "Opening GSSAPI connection with `%s'..." cmd)
	(erase-buffer)
	(let* ((coding-system-for-read 'binary)
	       (coding-system-for-write 'binary)
	       (process (start-process
			 name buffer shell-file-name shell-command-switch
			 (format-spec
			  cmd
			  (format-spec-make
			   ?s server
			   ?p (number-to-string port)
			   ?l user))))
	       response)
	  (when process
	    (while (and (memq (process-status process) '(open run))
			(goto-char (point-min))
			;; Athena IMTEST can output SSL verify errors
			(or (while (looking-at "^verify error:num=")
			      (forward-line))
			    t)
			(or (while (looking-at "^TLS connection established")
			      (forward-line))
			    t)
			;; cyrus 1.6.x (13? < x <= 22) queries capabilities
			(or (while (looking-at "^C:")
			      (forward-line))
			    t)
			;; cyrus 1.6 imtest print "S: " before server greeting
			(or (not (looking-at "S: "))
			    (forward-char 3)
			    t)
			;; GNU SASL may print 'Trying ...' first.
			(or (not (looking-at "Trying "))
			    (forward-line)
			    t)
			(not (and (looking-at "\\* \\(OK\\|PREAUTH\\|BYE\\) ")
				  ;; success in imtest 1.6:
				  (re-search-forward
				   (concat "^\\(\\(Authenticat.*\\)\\|\\("
					   "Client authentication "
					   "finished.*\\)\\)")
				   nil t)
				  (setq response (match-string 1)))))
	      (accept-process-output process 1)
	      (sit-for 1))
	    (erase-buffer)
	    (message "GSSAPI connection: %s" (or response "failed"))
	    (if (and response (let ((case-fold-search nil))
				(not (string-match "failed" response))))
		(setq done process)
	      (delete-process process)
	      nil))))
      done)))

(provide 'gssapi)

;;; gssapi.el ends here