Skip to content

Commit 3cb0c75

Browse files
aiudirogfrozencemetery
authored andcommitted
Configure Windows at import time
[[email protected]: Squash commits] Merges: #194
1 parent c3fc690 commit 3cb0c75

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

gssapi/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
low-level API functions.
2727
"""
2828

29+
import gssapi._win_config # noqa
30+
2931
from gssapi.raw.types import NameType, RequirementFlag, AddressType # noqa
3032
from gssapi.raw.types import MechType, IntEnumFlagSet # noqa
3133
from gssapi.raw.oids import OID # noqa

gssapi/_win_config.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Using GSSAPI on Windows requires having an installation of Kerberos for Windows
3+
(KfW) available in the user's PATH. This module should be imported before
4+
anything else to check for that installation, add it to the PATH if necessary,
5+
and throw any errors before they manifest as cryptic missing DLL errors later
6+
down the import tree.
7+
"""
8+
9+
import os
10+
import ctypes
11+
12+
#: Path to normal KfW installed bin folder
13+
KFW_BIN = os.path.join(
14+
os.environ.get('ProgramFiles', r'C:\Program Files'),
15+
'MIT', 'Kerberos', 'bin',
16+
)
17+
#: Download location for KfW
18+
KFW_DL = "https://web.mit.edu/KERBEROS/dist"
19+
20+
21+
def k4w_in_path():
22+
"""Return if the main GSSAPI DLL for KfW is available in the PATH"""
23+
try: # to load the main GSSAPI DLL
24+
ctypes.WinDLL('gssapi64.dll')
25+
except OSError: # DLL is not in PATH
26+
return False
27+
else: # DLL is in PATH, everything should work
28+
return True
29+
30+
31+
def error_not_found():
32+
"""Raise an OSError detailing that KfW is missing and how to get it"""
33+
raise OSError(
34+
"Could not find KfW installation. Please download and install "
35+
"the 64bit Kerberos for Windows MSI from %s and ensure the "
36+
"'bin' folder (%s) is in your PATH."
37+
% (KFW_DL, KFW_BIN)
38+
)
39+
40+
41+
def configure_windows():
42+
"""
43+
Validate that KfW appears to be installed correctly and add it to the
44+
PATH if necessary. In the case that it can't be located, raise an error.
45+
"""
46+
if k4w_in_path():
47+
return # All set, necessary DLLs should be available
48+
if os.path.exists(KFW_BIN): # In standard location
49+
os.environ['PATH'] += os.pathsep + KFW_BIN
50+
if k4w_in_path():
51+
return
52+
error_not_found()
53+
54+
55+
if os.name == 'nt': # Make sure we have the required DLLs
56+
configure_windows()

0 commit comments

Comments
 (0)