Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSSAPI support for IMAP #825

Open
wants to merge 1 commit into
base: current
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion i3pystatus/mail/imap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import contextlib
import time
import socket
import base64
from threading import Thread

from i3pystatus.mail import Backend
Expand All @@ -17,6 +18,41 @@
IMAP_EXCEPTIONS = (socket.error, socket.gaierror, IMAP4.abort, IMAP4.error)


class IMAPGSSMixin(object):
def gss_cb(self,challenge):
import kerberos
input_token = base64.b64encode(challenge).decode('utf-8')

if self.step == 0:
ret = kerberos.authGSSClientStep(self._gss_context, input_token)
if ret != kerberos.AUTH_GSS_CONTINUE:
self.step = 1
elif self.step == 1:
ret = kerberos.authGSSClientUnwrap(self._gss_context, input_token)
response = kerberos.authGSSClientResponse(self._gss_context)
ret = kerberos.authGSSClientWrap(self._gss_context, response, self.user)
response = kerberos.authGSSClientResponse(self._gss_context)

if response is None:
return ""
else:
return base64.b64decode(response)

def login_gssapi(self, user):
import kerberos
self.user = user
self.step = 0
_unused, self._gss_context = kerberos.authGSSClientInit("imap@{}".format(self.host))
return self.authenticate(b"GSSAPI", self.gss_cb)


class IMAP4_SSL(IMAP4_SSL, IMAPGSSMixin):
pass

class IMAP4(IMAP4, IMAPGSSMixin):
pass


class IMAP(Backend):
"""
Checks for mail on a IMAP server
Expand All @@ -27,6 +63,7 @@ class IMAP(Backend):
"username", "password",
('keyring_backend', 'alternative keyring backend for retrieving credentials'),
"ssl",
"gssapi",
"mailbox",
)
required = ("host", "username", "password")
Expand All @@ -35,6 +72,7 @@ class IMAP(Backend):
port = 993
ssl = True
mailbox = "INBOX"
gssapi = False

imap_class = IMAP4
connection = None
Expand All @@ -56,7 +94,10 @@ def ensure_connection(self):
self.connection.select(self.mailbox)
if not self.connection:
self.connection = self.imap_class(self.host, self.port)
self.connection.login(self.username, self.password)
if self.gssapi:
self.connection.login_gssapi(self.username)
else:
self.connection.login(self.username, self.password)
self.connection.select(self.mailbox)
yield
except IMAP_EXCEPTIONS:
Expand Down