<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14621] PyKerberos/trunk/test.py</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14621">14621</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2015-03-26 15:14:07 -0700 (Thu, 26 Mar 2015)</dd>
</dl>
<h3>Log Message</h3>
<pre>cleanup</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#PyKerberostrunktestpy">PyKerberos/trunk/test.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="PyKerberostrunktestpy"></a>
<div class="modfile"><h4>Modified: PyKerberos/trunk/test.py (14620 => 14621)</h4>
<pre class="diff"><span>
<span class="info">--- PyKerberos/trunk/test.py        2015-03-26 21:54:26 UTC (rev 14620)
+++ PyKerberos/trunk/test.py        2015-03-26 22:14:07 UTC (rev 14621)
</span><span class="lines">@@ -16,40 +16,44 @@
</span><span class="cx"> #
</span><span class="cx"> ##
</span><span class="cx">
</span><ins>+"""
+Examples:
+
+ sudo ./test.py -s HTTP@example.com service
+
+ sudo ./test.py -u user01 -p user01 -s HTTP@example.com -r EXAMPLE.COM basic
+
+ sudo ./test.py -s HTTP@example.com -r EXAMPLE.COM gssapi
+
+ ./test.py -s HTTP@example.com -h calendar.example.com -p 8008 server
+
+For the gssapi and server tests you will need to kinit a principal on the
+server first.
+"""
+
+from __future__ import print_function
+
</ins><span class="cx"> import kerberos
</span><span class="cx"> import getopt
</span><span class="cx"> import sys
</span><del>-import httplib
</del><span class="cx"> import socket
</span><span class="cx"> import ssl
</span><span class="cx">
</span><del>-"""
-Examples:
</del><ins>+from httplib import HTTPSConnection, HTTPConnection
</ins><span class="cx">
</span><del>-sudo ./test.py -s HTTP@example.com service
</del><span class="cx">
</span><del>-sudo ./test.py -u user01 -p user01 -s HTTP@example.com -r EXAMPLE.COM basic
</del><span class="cx">
</span><del>-sudo ./test.py -s HTTP@example.com -r EXAMPLE.COM gssapi
-
-./test.py -s HTTP@example.com -h calendar.example.com -p 8008 server
-
-For the gssapi and server tests you will need to kinit a principal on the server first.
-
-"""
-
</del><span class="cx"> def main():
</span><del>-
</del><span class="cx"> # Extract arguments
</span><span class="cx"> user = ""
</span><span class="cx"> pswd = ""
</span><span class="cx"> service = "HTTP@EXAMPLE.COM"
</span><span class="cx"> host = "host.example.com"
</span><del>- realm ="HOST.EXAMPLE.COM"
</del><ins>+ realm = "HOST.EXAMPLE.COM"
</ins><span class="cx"> port = 8008
</span><span class="cx"> use_ssl = False
</span><span class="cx"> allowedActions = ("service", "basic", "gssapi", "server",)
</span><del>-
</del><ins>+
</ins><span class="cx"> options, args = getopt.getopt(sys.argv[1:], "u:p:s:h:i:r:x")
</span><span class="cx">
</span><span class="cx"> for option, value in options:
</span><span class="lines">@@ -67,56 +71,68 @@
</span><span class="cx"> realm = value
</span><span class="cx"> elif option == "-x":
</span><span class="cx"> use_ssl = True
</span><del>-
</del><ins>+
</ins><span class="cx"> actions = set()
</span><span class="cx"> for arg in args:
</span><span class="cx"> if arg in allowedActions:
</span><span class="cx"> actions.add(arg)
</span><span class="cx"> else:
</span><del>- print "Action not allowed: %s" % (arg,)
</del><ins>+ print("Action not allowed: %s" % (arg,))
</ins><span class="cx"> sys.exit(1)
</span><del>-
</del><ins>+
</ins><span class="cx"> # Get service principal
</span><span class="cx"> if "service" in actions:
</span><del>- print "\n*** Running Service Principal test"
</del><ins>+ print("\n*** Running Service Principal test")
</ins><span class="cx"> s, h = service.split("@")
</span><del>- testServicePrincipal(s, h);
</del><ins>+ testServicePrincipal(s, h)
</ins><span class="cx">
</span><span class="cx"> # GSS Basic test
</span><span class="cx"> if "basic" in actions:
</span><span class="cx"> if (len(user) != 0) and (len(pswd) != 0):
</span><del>- print "\n*** Running basic test"
</del><ins>+ print("\n*** Running basic test")
</ins><span class="cx"> testCheckpassword(user, pswd, service, realm)
</span><span class="cx"> else:
</span><del>- print "\n*** Skipping basic test: no user or password specified"
</del><ins>+ print("\n*** Skipping basic test: no user or password specified")
</ins><span class="cx">
</span><span class="cx"> # Full GSSAPI test
</span><span class="cx"> if "gssapi" in actions:
</span><del>- print "\n*** Running GSSAPI test"
</del><ins>+ print("\n*** Running GSSAPI test")
</ins><span class="cx"> testGSSAPI(service)
</span><span class="cx">
</span><span class="cx"> if "server" in actions:
</span><del>- print "\n*** Running HTTP test"
</del><ins>+ print("\n*** Running HTTP test")
</ins><span class="cx"> testHTTP(host, port, use_ssl, service)
</span><span class="cx">
</span><del>- print "\n*** Done\n"
</del><ins>+ print("\n*** Done\n")
</ins><span class="cx">
</span><ins>+
+
</ins><span class="cx"> def testServicePrincipal(service, hostname):
</span><span class="cx"> try:
</span><span class="cx"> result = kerberos.getServerPrincipalDetails(service, hostname)
</span><span class="cx"> except kerberos.KrbError, e:
</span><del>- print "Kerberos service principal for %s/%s failed: %s" % (service, hostname, e[0])
</del><ins>+ print(
+ "Kerberos service principal for %s/%s failed: %s"
+ % (service, hostname, e[0])
+ )
</ins><span class="cx"> else:
</span><del>- print "Kerberos service principal for %s/%s succeeded: %s" % (service, hostname, result)
</del><ins>+ print(
+ "Kerberos service principal for %s/%s succeeded: %s"
+ % (service, hostname, result)
+ )
</ins><span class="cx">
</span><ins>+
+
</ins><span class="cx"> def testCheckpassword(user, pswd, service, realm):
</span><span class="cx"> try:
</span><span class="cx"> kerberos.checkPassword(user, pswd, service, realm)
</span><span class="cx"> except kerberos.BasicAuthError, e:
</span><del>- print "Kerberos authentication for %s failed: %s" % (user, e[0])
</del><ins>+ print("Kerberos authentication for %s failed: %s" % (user, e[0]))
</ins><span class="cx"> else:
</span><del>- print "Kerberos authentication for %s succeeded" % user
</del><ins>+ print("Kerberos authentication for %s succeeded" % user)
</ins><span class="cx">
</span><ins>+
+
</ins><span class="cx"> def testGSSAPI(service):
</span><span class="cx"> def statusText(r):
</span><span class="cx"> if r == 1:
</span><span class="lines">@@ -126,81 +142,89 @@
</span><span class="cx"> else:
</span><span class="cx"> return "Error"
</span><span class="cx">
</span><del>- rc, vc = kerberos.authGSSClientInit(service);
- print "Status for authGSSClientInit = %s" % statusText(rc);
</del><ins>+ rc, vc = kerberos.authGSSClientInit(service)
+ print("Status for authGSSClientInit = %s" % statusText(rc))
</ins><span class="cx"> if rc != 1:
</span><span class="cx"> return
</span><del>-
- rs, vs = kerberos.authGSSServerInit(service);
- print "Status for authGSSServerInit = %s" % statusText(rs);
</del><ins>+
+ rs, vs = kerberos.authGSSServerInit(service)
+ print("Status for authGSSServerInit = %s" % statusText(rs))
</ins><span class="cx"> if rs != 1:
</span><span class="cx"> return
</span><del>-
- rc = kerberos.authGSSClientStep(vc, "");
- print "Status for authGSSClientStep = %s" % statusText(rc);
</del><ins>+
+ rc = kerberos.authGSSClientStep(vc, "")
+ print("Status for authGSSClientStep = %s" % statusText(rc))
</ins><span class="cx"> if rc != 0:
</span><span class="cx"> return
</span><del>-
- rs = kerberos.authGSSServerStep(vs, kerberos.authGSSClientResponse(vc));
- print "Status for authGSSServerStep = %s" % statusText(rs);
</del><ins>+
+ rs = kerberos.authGSSServerStep(vs, kerberos.authGSSClientResponse(vc))
+ print("Status for authGSSServerStep = %s" % statusText(rs))
</ins><span class="cx"> if rs == -1:
</span><span class="cx"> return
</span><del>-
- rc = kerberos.authGSSClientStep(vc, kerberos.authGSSServerResponse(vs));
- print "Status for authGSSClientStep = %s" % statusText(rc);
</del><ins>+
+ rc = kerberos.authGSSClientStep(vc, kerberos.authGSSServerResponse(vs))
+ print("Status for authGSSClientStep = %s" % statusText(rc))
</ins><span class="cx"> if rc == -1:
</span><span class="cx"> return
</span><span class="cx">
</span><del>- 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);
</del><ins>+ print("Server user name: %s" % kerberos.authGSSServerUserName(vs))
+ print("Server target name: %s" % kerberos.authGSSServerTargetName(vs))
+ print("Client user name: %s" % kerberos.authGSSClientUserName(vc))
</ins><span class="cx">
</span><ins>+ rc = kerberos.authGSSClientClean(vc)
+ print("Status for authGSSClientClean = %s" % statusText(rc))
+
+ rs = kerberos.authGSSServerClean(vs)
+ print("Status for authGSSServerClean = %s" % statusText(rs))
+
+
+
</ins><span class="cx"> def testHTTP(host, port, use_ssl, service):
</span><span class="cx">
</span><del>- class HTTPSConnection_SSLv3(httplib.HTTPSConnection):
</del><ins>+ class HTTPSConnectionSSLv3(HTTPSConnection):
</ins><span class="cx"> "This class allows communication via SSL."
</span><span class="cx">
</span><span class="cx"> def connect(self):
</span><span class="cx"> "Connect to a host on a given (SSL) port."
</span><span class="cx">
</span><del>- 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)
</del><ins>+ 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
+ )
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> def sendRequest(host, port, ssl, method, uri, headers):
</span><span class="cx"> response = None
</span><span class="cx"> if use_ssl:
</span><del>- http = HTTPSConnection_SSLv3(host, port)
</del><ins>+ http = HTTPSConnectionSSLv3(host, port)
</ins><span class="cx"> else:
</span><del>- http = httplib.HTTPConnection(host, port)
</del><ins>+ http = HTTPConnection(host, port)
</ins><span class="cx"> try:
</span><span class="cx"> http.request(method, uri, "", headers)
</span><span class="cx"> response = http.getresponse()
</span><span class="cx"> finally:
</span><span class="cx"> http.close()
</span><del>-
</del><ins>+
</ins><span class="cx"> return response
</span><span class="cx">
</span><span class="cx"> # Initial request without auth header
</span><span class="cx"> uri = "/principals/"
</span><span class="cx"> response = sendRequest(host, port, use_ssl, "OPTIONS", uri, {})
</span><del>-
</del><ins>+
</ins><span class="cx"> if response is None:
</span><del>- print "Initial HTTP request to server failed"
</del><ins>+ print("Initial HTTP request to server failed")
</ins><span class="cx"> return
</span><del>-
</del><ins>+
</ins><span class="cx"> if response.status != 401:
</span><del>- print "Initial HTTP request did not result in a 401 response"
</del><ins>+ print("Initial HTTP request did not result in a 401 response")
</ins><span class="cx"> return
</span><del>-
</del><ins>+
</ins><span class="cx"> hdrs = response.msg.getheaders("www-authenticate")
</span><span class="cx"> if (hdrs is None) or (len(hdrs) == 0):
</span><del>- print "No www-authenticate header in initial HTTP response."
</del><ins>+ print("No www-authenticate header in initial HTTP response.")
</ins><span class="cx"> for hdr in hdrs:
</span><span class="cx"> hdr = hdr.strip()
</span><span class="cx"> splits = hdr.split(' ', 1)
</span><span class="lines">@@ -209,38 +233,47 @@
</span><span class="cx"> else:
</span><span class="cx"> break
</span><span class="cx"> else:
</span><del>- print "No www-authenticate header with negotiate in initial HTTP response."
- return
</del><ins>+ print(
+ "No www-authenticate header with negotiate in initial HTTP "
+ "response."
+ )
+ return
</ins><span class="cx">
</span><span class="cx"> try:
</span><del>- rc, vc = kerberos.authGSSClientInit(service=service);
</del><ins>+ rc, vc = kerberos.authGSSClientInit(service=service)
</ins><span class="cx"> except kerberos.GSSError, e:
</span><del>- print "Could not initialize GSSAPI: %s/%s" % (e[0][0], e[1][0])
</del><ins>+ print("Could not initialize GSSAPI: %s/%s" % (e[0][0], e[1][0]))
</ins><span class="cx"> return
</span><span class="cx">
</span><span class="cx"> try:
</span><del>- kerberos.authGSSClientStep(vc, "");
</del><ins>+ kerberos.authGSSClientStep(vc, "")
</ins><span class="cx"> except kerberos.GSSError, e:
</span><del>- print "Could not do GSSAPI step with continue: %s/%s" % (e[0][0], e[1][0])
</del><ins>+ print(
+ "Could not do GSSAPI step with continue: %s/%s"
+ % (e[0][0], e[1][0])
+ )
</ins><span class="cx"> return
</span><span class="cx">
</span><span class="cx"> hdrs = {}
</span><del>- hdrs["Authorization"] = "negotiate %s" % kerberos.authGSSClientResponse(vc)
</del><ins>+ hdrs["Authorization"] = "negotiate %s" % kerberos.authGSSClientResponse(vc)
</ins><span class="cx">
</span><span class="cx"> # Second request with auth header
</span><span class="cx"> response = sendRequest(host, port, use_ssl, "OPTIONS", uri, hdrs)
</span><del>-
</del><ins>+
</ins><span class="cx"> if response is None:
</span><del>- print "Second HTTP request to server failed"
</del><ins>+ print("Second HTTP request to server failed")
</ins><span class="cx"> return
</span><del>-
- if response.status/100 != 2:
- print "Second HTTP request did not result in a 2xx response: %d" % (response.status,)
</del><ins>+
+ if response.status / 100 != 2:
+ print(
+ "Second HTTP request did not result in a 2xx response: %d"
+ % (response.status,)
+ )
</ins><span class="cx"> return
</span><del>-
</del><ins>+
</ins><span class="cx"> hdrs = response.msg.getheaders("www-authenticate")
</span><span class="cx"> if (hdrs is None) or (len(hdrs) == 0):
</span><del>- print "No www-authenticate header in second HTTP response."
</del><ins>+ print("No www-authenticate header in second HTTP response.")
</ins><span class="cx"> return
</span><span class="cx"> for hdr in hdrs:
</span><span class="cx"> hdr = hdr.strip()
</span><span class="lines">@@ -250,23 +283,32 @@
</span><span class="cx"> else:
</span><span class="cx"> break
</span><span class="cx"> else:
</span><del>- print "No www-authenticate header with negotiate in second HTTP response."
- return
</del><ins>+ print(
+ "No www-authenticate header with negotiate in second HTTP "
+ "response."
+ )
+ return
</ins><span class="cx">
</span><span class="cx"> try:
</span><span class="cx"> kerberos.authGSSClientStep(vc, splits[1])
</span><span class="cx"> except kerberos.GSSError, e:
</span><del>- print "Could not verify server www-authenticate header in second HTTP response: %s/%s" % (e[0][0], e[1][0])
</del><ins>+ print(
+ "Could not verify server www-authenticate header in second HTTP "
+ "response: %s/%s"
+ % (e[0][0], e[1][0])
+ )
</ins><span class="cx"> return
</span><del>-
</del><ins>+
</ins><span class="cx"> try:
</span><del>- rc = kerberos.authGSSClientClean(vc);
</del><ins>+ kerberos.authGSSClientClean(vc)
</ins><span class="cx"> except kerberos.GSSError, e:
</span><del>- print "Could not clean-up GSSAPI: %s/%s" % (e[0][0], e[1][0])
</del><ins>+ print("Could not clean-up GSSAPI: %s/%s" % (e[0][0], e[1][0]))
</ins><span class="cx"> return
</span><span class="cx">
</span><del>- print "Authenticated successfully"
</del><ins>+ print("Authenticated successfully")
</ins><span class="cx"> return
</span><span class="cx">
</span><del>-if __name__=='__main__':
</del><ins>+
+
+if __name__ == "__main__":
</ins><span class="cx"> main()
</span></span></pre>
</div>
</div>
</body>
</html>