forked from bgamble/pykerberos
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.py
284 lines (233 loc) · 8.48 KB
/
test.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
##
# Copyright (c) 2006-2013 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##
import kerberos
import getopt
import sys
try:
import httplib
except ImportError as ex:
import http.client as httplib
import socket
import ssl
"""
Examples:
sudo ./test.py -s [email protected] service
sudo ./test.py -u user01 -p user01 -s [email protected] -r EXAMPLE.COM basic
sudo ./test.py -s [email protected] -r EXAMPLE.COM gssapi
./test.py -s [email protected] -h calendar.example.com -p 8008 server
For the gssapi and server tests you will need to kinit a principal on the server first.
"""
def main():
# Extract arguments
user = ""
pswd = ""
service = "[email protected]"
host = "host.example.com"
realm ="HOST.EXAMPLE.COM"
port = 8008
mech = None
use_ssl = False
allowedActions = ("service", "basic", "gssapi", "server",)
options, args = getopt.getopt(sys.argv[1:], "u:p:s:h:i:r:m:x")
for option, value in options:
if option == "-u":
user = value
elif option == "-p":
pswd = value
elif option == "-s":
service = value
elif option == "-h":
host = value
elif option == "-i":
port = value
elif option == "-r":
realm = value
elif option == "-m":
mech = value
elif option == "-x":
use_ssl = True
actions = set()
for arg in args:
if arg in allowedActions:
actions.add(arg)
else:
print("Action not allowed: %s" % (arg,))
sys.exit(1)
# Get service principal
if "service" in actions:
print("\n*** Running Service Principal test")
s, h = service.split("@")
testServicePrincipal(s, h)
# GSS Basic test
if "basic" in actions:
if (len(user) != 0) and (len(pswd) != 0):
print("\n*** Running basic test")
testCheckpassword(user, pswd, service, realm)
else:
print("\n*** Skipping basic test: no user or password specified")
# Full GSSAPI test
if "gssapi" in actions:
print("\n*** Running GSSAPI test")
testGSSAPI(service)
if "server" in actions:
print("\n*** Running HTTP test")
testHTTP(host, port, use_ssl, service, mech)
print("\n*** Done\n")
def testServicePrincipal(service, hostname):
try:
result = kerberos.getServerPrincipalDetails(service, hostname)
except kerberos.KrbError as e:
print("Kerberos service principal for %s/%s failed: %s" % (service, hostname, e[0]))
else:
print("Kerberos service principal for %s/%s succeeded: %s" % (service, hostname, result))
def testCheckpassword(user, pswd, service, realm):
try:
kerberos.checkPassword(user, pswd, service, realm)
except kerberos.BasicAuthError as e:
print("Kerberos authentication for %s failed: %s" % (user, e[0]))
else:
print("Kerberos authentication for %s succeeded" % user)
def testGSSAPI(service):
def statusText(r):
if r == 1:
return "Complete"
elif r == 0:
return "Continue"
else:
return "Error"
rc, vc = kerberos.authGSSClientInit(service)
print("Status for authGSSClientInit = %s" % statusText(rc))
if rc != 1:
return
rs, vs = kerberos.authGSSServerInit(service)
print("Status for authGSSServerInit = %s" % statusText(rs))
if rs != 1:
return
rc = kerberos.authGSSClientStep(vc, "")
print("Status for authGSSClientStep = %s" % statusText(rc))
if rc != 0:
return
rs = kerberos.authGSSServerStep(vs, kerberos.authGSSClientResponse(vc))
print("Status for authGSSServerStep = %s" % statusText(rs))
if rs == -1:
return
rc = kerberos.authGSSClientStep(vc, kerberos.authGSSServerResponse(vs))
print("Status for authGSSClientStep = %s" % statusText(rc))
if rc == -1:
return
print("Server user name: %s" % kerberos.authGSSServerUserName(vs))
print("Server target name: %s" % kerberos.authGSSServerTargetName(vs))
print("Client user name: %s" % kerberos.authGSSClientUserName(vc))
rc = kerberos.authGSSClientClean(vc)
print("Status for authGSSClientClean = %s" % statusText(rc))
rs = kerberos.authGSSServerClean(vs)
print("Status for authGSSServerClean = %s" % statusText(rs))
def testHTTP(host, port, use_ssl, service, mech):
class HTTPSConnection_SSLv3(httplib.HTTPSConnection):
"This class allows communication via SSL."
def connect(self):
"Connect to a host on a given (SSL) port."
sock = socket.create_connection((self.host, self.port), self.timeout)
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3)
def sendRequest(host, port, ssl, method, uri, headers):
response = None
if use_ssl:
http = HTTPSConnection_SSLv3(host, port)
else:
http = httplib.HTTPConnection(host, port)
try:
http.request(method, uri, "", headers)
response = http.getresponse()
finally:
http.close()
return response
# Initial request without auth header
uri = "/principals/"
response = sendRequest(host, port, use_ssl, "OPTIONS", uri, {})
if response is None:
print("Initial HTTP request to server failed")
return
if response.status != 401:
print("Initial HTTP request did not result in a 401 response")
return
hdrs = response.msg.getheaders("www-authenticate")
if (hdrs is None) or (len(hdrs) == 0):
print("No www-authenticate header in initial HTTP response.")
for hdr in hdrs:
hdr = hdr.strip()
splits = hdr.split(' ', 1)
if (len(splits) != 1) or (splits[0].lower() != "negotiate"):
continue
else:
break
else:
print("No www-authenticate header with negotiate in initial HTTP response.")
return
try:
mech_oid = None
if mech and mech.lower() == "krb5":
mech_oid = kerberos.GSS_MECH_OID_KRB5
elif mech and mech.lower() == "spnego":
mech_oid = kerberos.GSS_MECH_OID_SPNEGO
rc, vc = kerberos.authGSSClientInit(service=service, mech_oid=mech_oid)
except kerberos.GSSError as e:
print("Could not initialize GSSAPI: %s/%s" % (e[0][0], e[1][0]))
return
try:
kerberos.authGSSClientStep(vc, "")
except kerberos.GSSError as e:
print("Could not do GSSAPI step with continue: %s/%s" % (e[0][0], e[1][0]))
return
hdrs = {}
hdrs["Authorization"] = "negotiate %s" % kerberos.authGSSClientResponse(vc)
# Second request with auth header
response = sendRequest(host, port, use_ssl, "OPTIONS", uri, hdrs)
if response is None:
print("Second HTTP request to server failed")
return
if response.status/100 != 2:
print("Second HTTP request did not result in a 2xx response: %d" % (response.status,))
return
hdrs = response.msg.getheaders("www-authenticate")
if (hdrs is None) or (len(hdrs) == 0):
print("No www-authenticate header in second HTTP response.")
return
for hdr in hdrs:
hdr = hdr.strip()
splits = hdr.split(' ', 1)
if (len(splits) != 2) or (splits[0].lower() != "negotiate"):
continue
else:
break
else:
print("No www-authenticate header with negotiate in second HTTP response.")
return
try:
kerberos.authGSSClientStep(vc, splits[1])
except kerberos.GSSError as e:
print("Could not verify server www-authenticate header in second HTTP response: %s/%s" % (e[0][0], e[1][0]))
return
try:
kerberos.authGSSClientClean(vc)
except kerberos.GSSError as e:
print("Could not clean-up GSSAPI: %s/%s" % (e[0][0], e[1][0]))
return
print("Authenticated successfully")
return
if __name__=='__main__':
main()