gssapi.el 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ;;; gssapi.el --- GSSAPI/Kerberos 5 interface for Emacs
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Keywords: network
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'format-spec)
  20. (defcustom gssapi-program (list
  21. (concat "gsasl %s %p "
  22. "--mechanism GSSAPI "
  23. "--authentication-id %l")
  24. "imtest -m gssapi -u %l -p %p %s")
  25. "List of strings containing commands for GSSAPI (krb5) authentication.
  26. %s is replaced with server hostname, %p with port to connect to,
  27. and %l with the user name. The program should accept commands on
  28. stdin and return responses to stdout. Each entry in the list is
  29. tried until a successful connection is made."
  30. :version "24.1"
  31. :group 'network
  32. :type '(repeat string))
  33. (defun open-gssapi-stream (name buffer server port user)
  34. (let ((cmds gssapi-program)
  35. cmd done)
  36. (with-current-buffer buffer
  37. (while (and (not done)
  38. (setq cmd (pop cmds)))
  39. (message "Opening GSSAPI connection with `%s'..." cmd)
  40. (erase-buffer)
  41. (let* ((coding-system-for-read 'binary)
  42. (coding-system-for-write 'binary)
  43. (process (start-process
  44. name buffer shell-file-name shell-command-switch
  45. (format-spec
  46. cmd
  47. (format-spec-make
  48. ?s server
  49. ?p (number-to-string port)
  50. ?l user))))
  51. response)
  52. (when process
  53. (while (and (memq (process-status process) '(open run))
  54. (goto-char (point-min))
  55. ;; Athena IMTEST can output SSL verify errors
  56. (or (while (looking-at "^verify error:num=")
  57. (forward-line))
  58. t)
  59. (or (while (looking-at "^TLS connection established")
  60. (forward-line))
  61. t)
  62. ;; cyrus 1.6.x (13? < x <= 22) queries capabilities
  63. (or (while (looking-at "^C:")
  64. (forward-line))
  65. t)
  66. ;; cyrus 1.6 imtest print "S: " before server greeting
  67. (or (not (looking-at "S: "))
  68. (forward-char 3)
  69. t)
  70. ;; GNU SASL may print 'Trying ...' first.
  71. (or (not (looking-at "Trying "))
  72. (forward-line)
  73. t)
  74. (not (and (looking-at "\\* \\(OK\\|PREAUTH\\|BYE\\) ")
  75. ;; success in imtest 1.6:
  76. (re-search-forward
  77. (concat "^\\(\\(Authenticat.*\\)\\|\\("
  78. "Client authentication "
  79. "finished.*\\)\\)")
  80. nil t)
  81. (setq response (match-string 1)))))
  82. (accept-process-output process 1)
  83. (sit-for 1))
  84. (erase-buffer)
  85. (message "GSSAPI connection: %s" (or response "failed"))
  86. (if (and response (let ((case-fold-search nil))
  87. (not (string-match "failed" response))))
  88. (setq done process)
  89. (delete-process process)
  90. nil))))
  91. done)))
  92. (provide 'gssapi)
  93. ;;; gssapi.el ends here