954 lines
45 KiB
HTML
954 lines
45 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Sample: IBM JGSS non-JAAS server program" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahajgssdownloadsource.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="codeex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="intdatex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="intnumex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="intlocex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="accesspropertytest.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="putgetblobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="callex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="usingpositioneddelete.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="putgetclobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourceuse2.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind2.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="dataex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="putgetdatalinks.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="distinct.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtatxend.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="invalidconnect.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="basicjdbc.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtamulticonn.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourceuse.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="pmd.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="usingpositionedupdate.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="resultex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sensitive2.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sensitive.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="connectionpoolingsetup.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="exceptionexample.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtatxsuspend.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtatxeffect.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="connectionpoolingtest.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="statementpoolingtest.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="updateblobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="updateclobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtamultitx.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="useblobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="useclobs.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jtacommit.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="safegetudts.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="getconnections.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="prepex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="crdb2cac.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="stateex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jaaswrld.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sampthrd.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahajgsssrcl.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahajgssjacl.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahajgssjasr.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahajsseexmpls.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="callclex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="clcommex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jvlngex1.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="callcex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="calrpgex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="iostrmex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="invapiex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rzahapaseexmpls.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="jniex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="socketex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="runjpdex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sqljex.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sslcex01.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sslcex03.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sslcex02.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="sslcex04.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="rzahajgsssrsr" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Sample: IBM JGSS
|
|
non-JAAS server program</title>
|
|
</head>
|
|
<body id="rzahajgsssrsr"><a name="rzahajgsssrsr"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Sample: IBM JGSS
|
|
non-JAAS server program</h1>
|
|
<div><div class="section"><p>For more information about using the sample server program, see <a href="rzahajgssdownloadsource.htm">Downloading and running the IBM<sup>®</sup> JGSS samples</a>.</p>
|
|
<div class="note"><span class="notetitle">Note:</span> By
|
|
using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
<pre>// IBM JGSS 1.0 Sample Server Program
|
|
|
|
package com.ibm.security.jgss.test;
|
|
|
|
import org.ietf.jgss.*;
|
|
import com.ibm.security.jgss.Debug;
|
|
import java.io.*;
|
|
import java.net.*;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* A JGSS sample server; to be used in conjunction with a JGSS sample client.
|
|
*
|
|
* It continuously listens for client connections,
|
|
* spawning a thread to service an incoming connection.
|
|
* It is capable of running multiple threads concurrently.
|
|
* In other words, it can service multiple clients concurrently.
|
|
*
|
|
* Each thread first establishes a context with the client
|
|
* and then waits for a wrapped message followed by a MIC.
|
|
* It assumes that the client calculated the MIC over the plain
|
|
* text wrapped by the client.
|
|
*
|
|
* If the client delegates its credential to the server, the delegated
|
|
* credential is used to communicate with a secondary server.
|
|
*
|
|
* Also, the server can be started to act as a client as well as
|
|
* a server (using the -b option). In this case, the first
|
|
* thread spawned by the server uses the server principal's own credential
|
|
* to communicate with the secondary server.
|
|
*
|
|
* The secondary server must have been started prior to the (primary) server
|
|
* initiating contact with it (the scondary server).
|
|
* In communicating with the secondary server, the primary server acts as
|
|
* a JGSS initiator (i.e., client), establishing a context and engaging in
|
|
* wrap and MIC per-message exchanges with the secondary server.
|
|
*
|
|
* The server takes input parameters, and complements it
|
|
* with information from the jgss.ini file; any required input not
|
|
* supplied on the command line is taking from the jgss.ini file.
|
|
* Built-in defaults are used if there is no jgss.ini file or if a particular
|
|
* variable is not specified in the ini file.
|
|
*
|
|
* Usage: Server [options]
|
|
*
|
|
* The -? option produces a help message including supported options.
|
|
*
|
|
* This sample server does not use JAAS.
|
|
* It sets the JAVA variable
|
|
* javax.security.auth.useSubjectCredsOnly to false
|
|
* so that JGSS will not acquire credentials through JAAS.
|
|
* The server can be run against the JAAS sample clients and servers.
|
|
* See {@link JAASServer JAASServer} for a sample server that uses JAAS.
|
|
*/
|
|
|
|
class Server implements Runnable
|
|
{
|
|
/*
|
|
* NOTES:
|
|
* This class, Server, is expected to be run in concurrent
|
|
* multiple threads. The static variables consist of variables
|
|
* set from command-line arguments and variables (such as
|
|
* the server's own credentials, gssCred) that are set once during
|
|
* during initialization. These variables do not change
|
|
* once set and are shared between all running threads.
|
|
*
|
|
* The only static variable that is changed after being set initially
|
|
* is the variable 'beenInitiator' which is set 'true'
|
|
* by the first thread to run the server as initiator using
|
|
* the server's own creds. This ensures the server is run as an initiator
|
|
* once only. Querying and modifying 'beenInitiator' is synchronized
|
|
* between the threads.
|
|
*
|
|
* The variable 'tcp' is non-static and is set per thread
|
|
* to represent the socket on which the client being serviced
|
|
* by the thread connected.
|
|
*/
|
|
|
|
private static Util testUtil = null;
|
|
private static int myPort = 0;
|
|
private static Debug debug = new Debug();
|
|
private static String myName = null;
|
|
private static GSSCredential gssCred = null;
|
|
private static String serviceNameNoRealm = null;
|
|
private static String serviceHost = null;
|
|
private static int servicePort = 0;
|
|
private static String serviceMsg = null;
|
|
private static GSSManager mgr = null;
|
|
private static GSSName gssName = null;
|
|
private static String program = "Server";
|
|
private static boolean clientServer = false;
|
|
private static boolean primaryServer = true;
|
|
|
|
private static boolean beenInitiator = false;
|
|
|
|
private static final String usageString =
|
|
"\t[-?] [-# number] [-d | -n name] [-p port]"
|
|
+ "\n\t[-s serverName] [-h serverHost [:port]] [-P serverPort] [- msg]"
|
|
+ "\n"
|
|
+ "\n -?\t\t\thelp; produces this message"
|
|
+ "\n -# number\t\tWhether primary or secondary server"
|
|
+ " \n\t\t\t(1 = primary, 2 = secondary; default = first)"
|
|
+ "\n -n name\t\tthe server's principal name (without realm)"
|
|
+ "\n -p port\t\tthe port on which the server will be listening"
|
|
+ "\n -s serverName\t\tsecondary server's principal name"
|
|
+ " (without realm)"
|
|
+ "\n -h serverHost[:port]\tsecondary server's hostname"
|
|
+ " (and optional port number)"
|
|
+ "\n -P port\t\tsecondary server's port number"
|
|
+ "\n -m msg\t\tmessage to send to secondary server"
|
|
+ "\n -b \t\trun as both client and server"
|
|
+ " using the server's owns credentials";
|
|
|
|
// Non-static variables are thread-specific
|
|
// since each thread runs a separate instance of this class.
|
|
|
|
private String debugPrefix = null;
|
|
private TCPComms tcp = null;
|
|
|
|
static {
|
|
try {
|
|
testUtil = new Util();
|
|
} catch (Exception exc) {
|
|
exc.printStackTrace();
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
Server (Socket socket) throws Exception
|
|
{
|
|
debugPrefix = program + ": ";
|
|
tcp = new TCPComms(socket);
|
|
}
|
|
|
|
Server (String program) throws Exception
|
|
{
|
|
debugPrefix = program + ": ";
|
|
this.program = program;
|
|
}
|
|
|
|
Server (String program, boolean useSubjectCredsOnly) throws Exception
|
|
{
|
|
this(program);
|
|
setUseSubjectCredsOnly(useSubjectCredsOnly);
|
|
}
|
|
|
|
void setUseSubjectCredsOnly(boolean useSubjectCredsOnly)
|
|
{
|
|
final String subjectOnly = useSubjectCredsOnly ? "true" : "false";
|
|
final String property = "javax.security.auth.useSubjectCredsOnly";
|
|
|
|
String temp = (String)java.security.AccessController.doPrivileged(
|
|
new sun.security.action.GetPropertyAction(property));
|
|
|
|
if (temp == null)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "setting useSubjectCredsOnly property to "
|
|
+ (useSubjectCredsOnly ? "true" : "false"));
|
|
|
|
// Property not set. Set it to the specified value.
|
|
|
|
java.security.AccessController.doPrivileged(
|
|
new java.security.PrivilegedAction() {
|
|
public Object run() {
|
|
System.setProperty(property, subjectOnly);
|
|
return null;
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "useSubjectCredsOnly property already set "
|
|
+ "in JVM to " + temp);
|
|
}
|
|
}
|
|
|
|
private void init(boolean primary,
|
|
String myNameWithoutRealm,
|
|
int port,
|
|
String serverNameWithoutRealm,
|
|
String serverHostname,
|
|
int serverPort,
|
|
String message,
|
|
boolean clientServer)
|
|
throws Exception
|
|
{
|
|
primaryServer = primary;
|
|
this.clientServer = clientServer;
|
|
|
|
myName = myNameWithoutRealm;
|
|
|
|
// my port
|
|
if (port > 0)
|
|
{
|
|
myPort = port;
|
|
}
|
|
else if (primary)
|
|
{
|
|
myPort = testUtil.getDefaultServicePort();
|
|
}
|
|
else
|
|
{
|
|
myPort = testUtil.getDefaultService2Port();
|
|
}
|
|
|
|
if (primary)
|
|
{
|
|
///// peer's name
|
|
if (serverNameWithoutRealm != null)
|
|
{
|
|
serviceNameNoRealm = serverNameWithoutRealm;
|
|
}
|
|
else
|
|
{
|
|
serviceNameNoRealm =
|
|
testUtil.getDefaultService2PrincipalWithoutRealm();
|
|
}
|
|
|
|
// peer's host
|
|
if (serverHostname != null)
|
|
{
|
|
if (serverHostname.equalsIgnoreCase("localHost"))
|
|
{
|
|
serverHostname = InetAddress.getLocalHost().getHostName();
|
|
}
|
|
|
|
serviceHost = serverHostname;
|
|
}
|
|
else
|
|
{
|
|
serviceHost = testUtil.getDefaultService2Hostname();
|
|
}
|
|
|
|
// peer's port
|
|
if (serverPort > 0)
|
|
{
|
|
servicePort = serverPort;
|
|
}
|
|
else
|
|
{
|
|
servicePort = testUtil.getDefaultService2Port();
|
|
}
|
|
|
|
// message for peer
|
|
if (message != null)
|
|
{
|
|
serviceMsg = message;
|
|
}
|
|
else
|
|
{
|
|
serviceMsg = "Hi there! I am a server."
|
|
+ "But I can be a client, too";
|
|
}
|
|
}
|
|
|
|
String temp = debugPrefix + "details"
|
|
+ "\n\tPrimary:\t" + primary
|
|
+ "\n\tName:\t\t" + myName
|
|
+ "\n\tPort:\t\t" + myPort
|
|
+ "\n\tClient+server:\t" + clientServer;
|
|
if (primary)
|
|
{
|
|
temp += "\n\tOther Server:"
|
|
+ "\n\t\tName:\t" + serviceNameNoRealm
|
|
+ "\n\t\tHost:\t" + serviceHost
|
|
+ "\n\t\tPort:\t" + servicePort
|
|
+ "\n\t\tMsg:\t" + serviceMsg;
|
|
}
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, temp);
|
|
}
|
|
|
|
|
|
void initialize() throws GSSException
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "creating GSSManager");
|
|
|
|
mgr = GSSManager.getInstance();
|
|
|
|
int usage = clientServer ? GSSCredential.INITIATE_AND_ACCEPT
|
|
: GSSCredential.ACCEPT_ONLY;
|
|
|
|
if (myName != null)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "creating GSSName for " + myName);
|
|
|
|
gssName = mgr.createName(myName,
|
|
GSSName.NT_HOSTBASED_SERVICE);
|
|
|
|
Oid krb5MechanismOid = new Oid("1.2.840.113554.1.2.2");
|
|
gssName.canonicalize(krb5MechanismOid);
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "Canonicalized GSSName=" + gssName);
|
|
}
|
|
else
|
|
gssName = null;
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "creating"
|
|
+ ((gssName == null)? " default " : " ")
|
|
+ "credential");
|
|
|
|
gssCred = mgr.createCredential(
|
|
gssName, GSSCredential.DEFAULT_LIFETIME,
|
|
(Oid)null, usage);
|
|
if (gssName == null)
|
|
{
|
|
gssName = gssCred.getName();
|
|
myName = gssName.toString();
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "default credential principal=" + myName);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void processArgs(String[] args) throws Exception
|
|
{
|
|
String port = null;
|
|
String name = null;
|
|
int iport = 0;
|
|
|
|
String sport = null;
|
|
int isport = 0;
|
|
String sname = null;
|
|
String shost = null;
|
|
String smessage = null;
|
|
|
|
boolean primary = true;
|
|
String status = null;
|
|
|
|
boolean defaultPrinc = false;
|
|
boolean clientServer = false;
|
|
|
|
GetOptions options = new GetOptions(args, "?#:p:n:P:s:h:m:b");
|
|
int ch = -1;
|
|
while ((ch = options.getopt()) != options.optEOF)
|
|
{
|
|
switch(ch)
|
|
{
|
|
case '?':
|
|
printUsage();
|
|
System.exit(1);
|
|
|
|
case '#':
|
|
if (status == null)
|
|
status = options.optArgGet();
|
|
continue;
|
|
|
|
case 'p':
|
|
if (port == null)
|
|
port = options.optArgGet();
|
|
continue;
|
|
|
|
case 'n':
|
|
if (name == null)
|
|
name = options.optArgGet();
|
|
continue;
|
|
|
|
case 'b':
|
|
clientServer = true;
|
|
continue;
|
|
|
|
////// The other server
|
|
|
|
case 'P':
|
|
if (sport == null)
|
|
sport = options.optArgGet();
|
|
continue;
|
|
|
|
case 'm':
|
|
if (smessage == null)
|
|
smessage = options.optArgGet();
|
|
continue;
|
|
|
|
case 's':
|
|
if (sname == null)
|
|
sname = options.optArgGet();
|
|
continue;
|
|
|
|
case 'h':
|
|
if (shost == null)
|
|
{
|
|
shost = options.optArgGet();
|
|
int p = shost.indexOf(':');
|
|
if (p != -1)
|
|
{
|
|
String temp1 = shost.substring(0, p);
|
|
if (sport == null)
|
|
sport = shost.substring
|
|
(p+1, shost.length()).trim();
|
|
shost = temp1;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (defaultPrinc && (name != null))
|
|
{
|
|
System.out.println(
|
|
"ERROR: '-d' and '-n ' options are mutually exclusive");
|
|
printUsage();
|
|
System.exit(1);
|
|
}
|
|
|
|
if (status != null)
|
|
{
|
|
int p = -1;
|
|
try {
|
|
p = Integer.parseInt(status);
|
|
} catch (Exception exc) {
|
|
System.out.println( "Bad status input: "+status);
|
|
}
|
|
|
|
if (p != -1)
|
|
{
|
|
primary = (p == 1);
|
|
}
|
|
}
|
|
|
|
if (port != null)
|
|
{
|
|
int p = -1;
|
|
try {
|
|
p = Integer.parseInt(port);
|
|
} catch (Exception exc) {
|
|
System.out.println( "Bad port input: "+port);
|
|
}
|
|
if (p != -1)
|
|
iport = p;
|
|
}
|
|
|
|
if (sport != null)
|
|
{
|
|
int p = -1;
|
|
try {
|
|
p = Integer.parseInt(sport);
|
|
} catch (Exception exc) {
|
|
System.out.println( "Bad server port input: "+port);
|
|
}
|
|
if (p != -1)
|
|
isport = p;
|
|
}
|
|
|
|
init(primary, // first or second server
|
|
name, // my name
|
|
iport, // my port
|
|
sname, // other server's name
|
|
shost, // other server's hostname
|
|
isport, // other server's port
|
|
smessage, // msg for other server
|
|
clientServer); // whether to run as initiator with own creds
|
|
}
|
|
|
|
void processRequests() throws Exception
|
|
{
|
|
ServerSocket ssocket = null;
|
|
Server server = null;
|
|
try {
|
|
ssocket = new ServerSocket(myPort);
|
|
do {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "listening on port " + myPort + " ...");
|
|
Socket csocket = ssocket.accept();
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "incoming connection on " + csocket);
|
|
|
|
server = new Server(csocket); // set client socket per thread
|
|
Thread thread = new Thread(server);
|
|
thread.start();
|
|
if (!thread.isAlive())
|
|
server.dispose(); // close the client socket
|
|
} while(true);
|
|
} catch (Exception exc) {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "*** ERROR processing requests ***");
|
|
exc.printStackTrace();
|
|
} finally {
|
|
try {
|
|
if (ssocket != null)
|
|
ssocket.close(); // close the server socket
|
|
if (server != null)
|
|
server.dispose(); // close the client socket
|
|
} catch (Exception exc) {}
|
|
}
|
|
}
|
|
|
|
void dispose()
|
|
{
|
|
try {
|
|
if (tcp != null)
|
|
{
|
|
tcp.close();
|
|
tcp = null;
|
|
}
|
|
} catch (Exception exc) {}
|
|
}
|
|
|
|
boolean establishContext(GSSContext context) throws Exception
|
|
{
|
|
byte[] response = null;
|
|
byte[] request = null;
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "establishing context");
|
|
|
|
do {
|
|
request = tcp.receive();
|
|
if (request == null || request.length == 0)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "Received no data; perhaps client disconnected");
|
|
|
|
return false;
|
|
}
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "accepting");
|
|
if ((response = context.acceptSecContext
|
|
(request, 0, request.length)) != null)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "sending response");
|
|
tcp.send(response);
|
|
}
|
|
} while(!context.isEstablished());
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "context established - " + context);
|
|
|
|
return true;
|
|
}
|
|
|
|
byte[] unwrap(GSSContext context, byte[] msg) throws Exception
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "unwrapping");
|
|
|
|
MessageProp mp = new MessageProp(true);
|
|
byte[] unwrappedMsg = context.unwrap(msg, 0, msg.length, mp);
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "unwrapped msg is:");
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, unwrappedMsg);
|
|
|
|
return unwrappedMsg;
|
|
}
|
|
|
|
void verifyMIC (GSSContext context, byte[] mic, byte[] raw) throws Exception
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "verifying MIC");
|
|
|
|
MessageProp mp = new MessageProp(true);
|
|
context.verifyMIC(mic, 0, mic.length, raw, 0, raw.length, mp);
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "successfully verified MIC");
|
|
}
|
|
|
|
void useDelegatedCred(GSSContext context) throws Exception
|
|
{
|
|
GSSCredential delCred = context.getDelegCred();
|
|
if (delCred != null)
|
|
{
|
|
if (primaryServer)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix +
|
|
"Primary server received delegated cred; using it");
|
|
runAsInitiator(delCred); // using delegated creds
|
|
}
|
|
else
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix +
|
|
"Non-primary server received delegated cred; "
|
|
+ "ignoring it");
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix +
|
|
"ERROR: null delegated cred");
|
|
}
|
|
}
|
|
|
|
public void run()
|
|
{
|
|
byte[] response = null;
|
|
byte[] request = null;
|
|
boolean unwrapped = false;
|
|
GSSContext context = null;
|
|
|
|
try {
|
|
Thread currentThread = Thread.currentThread();
|
|
String threadName = currentThread.getName();
|
|
|
|
debugPrefix = program + " " + threadName + ": ";
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "servicing client ...");
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "creating GSSContext");
|
|
|
|
context = mgr.createContext(gssCred);
|
|
|
|
// First establish context with the initiator.
|
|
if (!establishContext(context))
|
|
return;
|
|
|
|
// Then process messages from the initiator.
|
|
// We expect to receive a wrapped message followed by a MIC.
|
|
// The MIC should have been calculated over the plain
|
|
// text that we received wrapped.
|
|
// Use delegated creds if any.
|
|
// Then run as initiator using own creds if necessary; only
|
|
// the first thread does this.
|
|
|
|
do {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "receiving per-message request");
|
|
|
|
request = tcp.receive();
|
|
if (request == null || request.length == 0)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "Received no data; perhaps client disconnected");
|
|
|
|
return;
|
|
}
|
|
|
|
// Expect wrapped message first.
|
|
if (!unwrapped)
|
|
{
|
|
response = unwrap(context, request);
|
|
unwrapped = true;
|
|
continue; // get next request
|
|
}
|
|
|
|
// Followed by a MIC.
|
|
verifyMIC(context, request, response);
|
|
|
|
// Impersonate the initiator if it delegated its creds to us.
|
|
if (context.getCredDelegState())
|
|
useDelegatedCred(context);
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "clientServer=" + clientServer
|
|
+ ", beenInitiator=" + beenInitiator);
|
|
|
|
// If necessary, run as initiator using our own creds.
|
|
if (clientServer)
|
|
runAsInitiatorOnce(currentThread);
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "done");
|
|
return;
|
|
|
|
} while(true);
|
|
|
|
} catch (Exception exc) {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix + "ERROR");
|
|
exc.printStackTrace();
|
|
|
|
// Squelch per-thread exceptions so we don't bring
|
|
// the server down because of exceptions in
|
|
// individual threads.
|
|
return;
|
|
} finally {
|
|
if (context != null)
|
|
{
|
|
try {
|
|
context.dispose();
|
|
} catch (Exception exc) {}
|
|
}
|
|
}
|
|
}
|
|
|
|
synchronized void runAsInitiatorOnce(Thread thread)
|
|
throws InterruptedException
|
|
{
|
|
if (!beenInitiator)
|
|
{
|
|
// set flag true early to prevent subsequent threads
|
|
// from attempting to runAsInitiator.
|
|
beenInitiator = true;
|
|
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix +
|
|
"About to run as initiator with own creds ...");
|
|
|
|
//thread.sleep(30*1000, 0);
|
|
runAsInitiator();
|
|
}
|
|
}
|
|
|
|
|
|
void runAsInitiator(GSSCredential cred)
|
|
{
|
|
Client client = null;
|
|
try {
|
|
client = new Client(cred,
|
|
serviceNameNoRealm,
|
|
serviceHost,
|
|
servicePort,
|
|
serviceMsg);
|
|
|
|
client.initialize();
|
|
|
|
BitSet flags = new BitSet();
|
|
flags.set(Util.CONTEXT_OPTS_MUTUAL);
|
|
flags.set(Util.CONTEXT_OPTS_CONF);
|
|
flags.set(Util.CONTEXT_OPTS_INTEG);
|
|
|
|
client.interactWithAcceptor(flags);
|
|
|
|
} catch (Exception exc) {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "Exception running as initiator");
|
|
|
|
exc.printStackTrace();
|
|
} finally {
|
|
try {
|
|
client.dispose();
|
|
} catch (Exception exc) {}
|
|
}
|
|
}
|
|
|
|
void runAsInitiator()
|
|
{
|
|
if (clientServer)
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION,
|
|
debugPrefix + "running as initiator with own creds");
|
|
|
|
runAsInitiator(gssCred); // use own creds;
|
|
}
|
|
else
|
|
{
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, debugPrefix
|
|
+ "Cannot run as initiator with own creds "
|
|
+ "\nbecause not running as both initiator and acceptor.");
|
|
}
|
|
}
|
|
|
|
void printUsage()
|
|
{
|
|
System.out.println(program + usageString);
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception
|
|
{
|
|
System.out.println(debug.toString()); // XXXXXXX
|
|
String programName = "Server";
|
|
try {
|
|
Server server = new Server(programName,
|
|
false); // don't use creds from Subject
|
|
server.processArgs(args);
|
|
server.initialize();
|
|
server.processRequests();
|
|
} catch (Exception exc) {
|
|
debug.out(Debug.OPTS_CAT_APPLICATION, programName + ": EXCEPTION");
|
|
exc.printStackTrace();
|
|
throw exc;
|
|
}
|
|
}
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="codeex.htm" title="The following is a list of code examples for the IBM Developer Kit for Java.">Code examples for the IBM Developer Kit for Java</a></div>
|
|
</div>
|
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
|
<div><a href="rzahapaseexmpls.htm" title="The IBM i5/OS PASE native method for Java example calls an instance of a native C method that then uses Java Native Interface (JNI) to call back into Java code. Rather than accessing the string directly from Java code, the example calls a native method that then calls back into Java through JNI to get the string value.">Example: IBM i5/OS PASE native method for Java</a></div>
|
|
</div>
|
|
<div class="reltasks"><strong>Related tasks</strong><br />
|
|
<div><a href="runjpdex.htm" title="You can either use the iSeries command line or the Qshell environment to run the Java Performance Data Converter (JPDC).">Example: Run the Java Performance Data Converter</a></div>
|
|
</div>
|
|
<div class="relref"><strong>Related reference</strong><br />
|
|
<div><a href="intdatex.htm" title="This example shows how you can use locales to format dates.">Example: Internationalization of dates using the java.util.DateFormat class</a></div>
|
|
<div><a href="intnumex.htm" title="This example shows how you can use locales to format numbers.">Example: Internationalization of numeric display using the java.util.NumberFormat class</a></div>
|
|
<div><a href="intlocex.htm" title="This example shows how you can use locales with resource bundles to internationalize program strings.">Example: Internationalization of locale-specific data using the java.util.ResourceBundle class</a></div>
|
|
<div><a href="accesspropertytest.htm" title="This is an example of how to use the Access property.">Example: Access property</a></div>
|
|
<div><a href="putgetblobs.htm" title="This is an example of how a BLOB can be put into the database or retrieved from the database.">Example: BLOB</a></div>
|
|
<div><a href="callex.htm" title="This is an example of how to use the CallableStatement interface.">Example: CallableStatement interface for IBM Developer Kit for Java</a></div>
|
|
<div><a href="usingpositioneddelete.htm" title="This is an example of how to remove values from a table through another statement's cursor.">Example: Remove values from a table through another statement's cursor</a></div>
|
|
<div><a href="putgetclobs.htm" title="This is an example of how a CLOB can be put into the database or retrieved from the database.">Example: CLOB</a></div>
|
|
<div><a href="udbdatasourcebind.htm" title="This is an example of how to create a UDBDataSource and get it bound with JNDI.">Example: Create a UDBDataSource and bind it with JNDI</a></div>
|
|
<div><a href="udbdatasourceuse2.htm" title="This is an example of how to create a UDBDataSource, and use the getConnection method to obtain a user ID and password at runtime.">Example: Create a UDBDataSource, and obtain a user ID and password</a></div>
|
|
<div><a href="udbdatasourcebind2.htm" title="This is an example of how to create a UDBDataSource, and set the user ID and password as DataSource properties.">Example: Create a UDBDataSourceBind and set DataSource properties</a></div>
|
|
<div><a href="dataex.htm" title="This example shows how to return a list of tables.">Example: DatabaseMetaData interface for IBM Developer Kit for Java - Return a list of tables</a></div>
|
|
<div><a href="putgetdatalinks.htm" title="This is an example of how to use datalinks in your applications.">Example: Datalink</a></div>
|
|
<div><a href="distinct.htm" title="This is an example of how to use distinct types.">Example: Distinct types</a></div>
|
|
<div><a href="sqljex.htm" title="The following example SQLJ application, App.sqlj, uses static SQL to retrieve and update data from the EMPLOYEE table of the DB2 sample database.">Example: Embed SQL Statements in your Java application</a></div>
|
|
<div><a href="jtatxend.htm" title="This is an example of ending a transaction in your application.">Example: End a transaction</a></div>
|
|
<div><a href="invalidconnect.htm" title="This is an example of how to use the Connection property in SQL naming mode.">Example: Invalid user ID and password</a></div>
|
|
<div><a href="basicjdbc.htm" title="This is an example of how to use the BasicJDBC program.">Example: JDBC</a></div>
|
|
<div><a href="jtamulticonn.htm" title="This is an example of how to use multiple connections working on a single transaction.">Example: Multiple connections that work on a transaction</a></div>
|
|
<div><a href="udbdatasourceuse.htm" title="The following example obtains an initial context before binding the UDBDataSource. The lookup method is then used on that context to return an object of type DataSource for the application to use.">Example: Obtain an initial context before binding UDBDataSource</a></div>
|
|
<div><a href="pmd.htm" title="This is an example of using the ParameterMetaData interface to retrieve information about parameters.">Example: ParameterMetaData</a></div>
|
|
<div><a href="usingpositionedupdate.htm" title="This is an example of how to change values with a statement through another statement's cursor.">Example: Change values with a statement through another statement's cursor</a></div>
|
|
<div><a href="resultex.htm" title="This is an example of how to use the ResultSet interface.">Example: ResultSet interface for IBM Developer Kit for Java</a></div>
|
|
<div><a href="sensitive2.htm" title="The following example shows how a change can affect a where clause of an SQL statement based on the sensitivity of the ResultSet.">Example: ResultSet sensitivity</a></div>
|
|
<div><a href="sensitive.htm" title="The following example shows the difference between sensitive and insensitive ResultSets when rows are inserted into a table.">Example: Sensitive and insensitive ResultSets</a></div>
|
|
<div><a href="connectionpoolingsetup.htm" title="This is an example of how to use connection pooling with UDBDataSource and UDBConnectionPoolDataSource.">Example: Set up connection pooling with UDBDataSource and UDBConnectionPoolDataSource</a></div>
|
|
<div><a href="exceptionexample.htm" title="This is an example of catching an SQLException and dumping all the information that it provides.">Example: SQLException</a></div>
|
|
<div><a href="jtatxsuspend.htm" title="This is an example of a transaction that is suspended and then is resumed.">Example: Suspend and resume a transaction</a></div>
|
|
<div><a href="jtatxeffect.htm" title="This is an example of the how a Statement object is reprocessed under another transaction to perform work.">Example: Suspended ResultSets</a></div>
|
|
<div><a href="connectionpoolingtest.htm" title="This is an example of how to test the performance of the pooling example against the performance of the non-pooling example.">Example: Test the performance of connection pooling</a></div>
|
|
<div><a href="statementpoolingtest.htm" title="This is an example of testing one DataSource that uses connection pooling only and the other DataSource that uses statement and connection pooling.">Example: Test the performance of two DataSources</a></div>
|
|
<div><a href="updateblobs.htm" title="This is an example of how to update BLOBs in your applications.">Example: Update BLOBs</a></div>
|
|
<div><a href="updateclobs.htm" title="This is an example of how to update CLOBs in your applications.">Example: Update CLOBs</a></div>
|
|
<div><a href="jtamultitx.htm" title="This is an example of how to use a single connection with multiple transactions.">Example: Use a connection with multiple transactions</a></div>
|
|
<div><a href="useblobs.htm" title="This is an example of how to use BLOBs in your applications.">Example: Use BLOBs</a></div>
|
|
<div><a href="useclobs.htm" title="This is an example of how to use CLOBs in your applications.">Example: Use CLOBs</a></div>
|
|
<div><a href="jtacommit.htm" title="This is an example of how to use the Java Transaction API (JTA) to handle a transaction in an application.">Example: Use JTA to handle a transaction</a></div>
|
|
<div><a href="safegetudts.htm" title="This is an example of how to use metadata ResultSets that have more than one column.">Example: Use metadata ResultSets that have more than one column</a></div>
|
|
<div><a href="getconnections.htm" title="This is an example of how to use the native JDBC connection and the IBM Toolbox for Java JDBC connection in a program.">Example: Use native JDBC and IBM Toolbox for Java JDBC concurrently</a></div>
|
|
<div><a href="prepex.htm" title="This is an example of using a PreparedStatement object's executeQuery method to obtain a ResultSet.">Example: Use PreparedStatement to obtain a ResultSet</a></div>
|
|
<div><a href="crdb2cac.htm" title="There are several ways to place data into a DB2CachedRowSet.">Create and populate a DB2CachedRowSet</a></div>
|
|
<div><a href="stateex.htm" title="This is an example of how to use the Statement object's executeUpdate method.">Example: Use the Statement object's executeUpdate method</a></div>
|
|
<div><a href="jaaswrld.htm" title="These examples show you the three files that are needed to compile and run HelloWorld for JAAS.">Examples: JAAS HelloWorld</a></div>
|
|
<div><a href="sampthrd.htm">Example: JAAS SampleThreadSubjectLogin</a></div>
|
|
<div><a href="rzahajgsssrcl.htm">Sample: IBM JGSS non-JAAS client program</a></div>
|
|
<div><a href="rzahajgssjacl.htm">Sample: IBM JGSS JAAS-enabled client program</a></div>
|
|
<div><a href="rzahajgssjasr.htm">Sample: IBM JGSS JAAS-enabled server program</a></div>
|
|
<div><a href="rzahajsseexmpls.htm" title="The JSSE examples show how a client and a server can use the native iSeries JSSE provider to create a context that enables secure communications.">Examples: IBM Java Secure Sockets Extension</a></div>
|
|
<div><a href="callclex.htm" title="This example shows how to run CL programs from within a Java program. In this example, the Java class CallCLPgm runs a CL program.">Example: Call a CL program with java.lang.Runtime.exec()</a></div>
|
|
<div><a href="clcommex.htm" title="This example shows how to run a control language (CL) command from within a Java program.">Example: Call a CL command with java.lang.Runtime.exec()</a></div>
|
|
<div><a href="jvlngex1.htm" title="This example shows how to call another Java program with java.lang.Runtime.exec(). This class calls the Hello program that is shipped as part of the IBM Developer Kit for Java. When the Hello class writes to System.out, this program gets a handle to the stream and can read from it.">Example: Call another Java program with java.lang.Runtime.exec()</a></div>
|
|
<div><a href="callcex.htm" title="This is an example of a C program that uses the system() function to call the Java Hello program.">Example: Call Java from C</a></div>
|
|
<div><a href="calrpgex.htm" title="This is an example of an RPG program that uses the QCMDEXC API to call the Java Hello program.">Example: Call Java from RPG</a></div>
|
|
<div><a href="iostrmex.htm" title="This example shows how to call a C program from Java and use input and output streams for interprocess communication.">Example: Use input and output streams for interprocess communication</a></div>
|
|
<div><a href="invapiex.htm" title="This example follows the standard Invocation API paradigm.">Example: Java Invocation API</a></div>
|
|
<div><a href="jniex.htm" title="This example program is a simple Java Native Interface (JNI) example in which a C native method is used to display "Hello, World." Use the javah tool with the NativeHello class file to generate the NativeHello.h file. This example assumes that the NativeHello C implementation is part of a service program that is called NATHELLO.">Examples: Use the Java Native Interface for native methods</a></div>
|
|
<div><a href="socketex.htm" title="This example uses sockets to communicate between a Java program and a C program.">Example: Use sockets for interprocess communication</a></div>
|
|
<div><a href="sslcex01.htm" title="These examples show you how to change a simple socket class, named simpleSocketClient, so that it uses socket factories to create all of the sockets. The first example shows you the simpleSocketClient class without socket factories. The second example shows you the simpleSocketClient class with socket factories. In the second example, simpleSocketClient is renamed to factorySocketClient.">Examples: Change your Java code to use client socket factories</a></div>
|
|
<div><a href="sslcex03.htm" title="These examples show you how to change a simple socket class, named simpleSocketServer, so that it uses socket factories to create all of the sockets. The first example shows you the simpleSocketServer class without socket factories. The second example shows you the simpleSocketServer class with socket factories. In the second example, simpleSocketServer is renamed to factorySocketServer.">Examples: Change your Java code to use server socket factories</a></div>
|
|
<div><a href="sslcex02.htm" title="These examples show you how to change one class, named factorySocketClient, to use secure sockets layer (SSL). The first example shows you the factorySocketClient class not using SSL. The second example shows you the same class, renamed factorySSLSocketClient, using SSL.">Examples: Change your Java client to use secure sockets layer</a></div>
|
|
<div><a href="sslcex04.htm" title="These examples show you how to change one class, named factorySocketServer, to use secure sockets layer (SSL).">Examples: Change your Java server to use secure sockets layer</a></div>
|
|
</div>
|
|
|
|
<div class="linklist"><strong>Collected links</strong><br />
|
|
|
|
<div><a href="rzahajgssdownloadsource.htm" title="This topic contains instructions for downloading and running the sample javadoc information.">Downloading and running
|
|
the IBM JGSS samples</a></div>
|
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div></div>
|
|
</div>
|
|
</body>
|
|
</html> |